Regular Expression

Hello

Can someone please tell me the regex that would extract the word hello from the following string:


{{hello}}

or uk_london from the following string.


{{uk_London}}

Thanks in advance

Is it always {{something}}?

if that’s the case then:


{{(.*)}}

Period . is a wildcard. Star * means 0 or many. So, {{0 or many of anything other than }}. If you want at least one, use + instead of .
Parenthesis is just a logical part, it’s accessed via $1 in many cases. You can do each if you wanted to ({{)(.
)(}}) and access each piece respectively $1, $2, $3.

If you want just letters:


{{([a-zA-Z]*)}}

or letters and numbers


{{([a-zA-Z\\d]*)}}

Hello,

First of all many thanks for the help. It works like a charm. Here is what I wanted to do and have achieved using the regex. All I need is your suggestion if this is the correct way of doing it.

I want to read a text file (a template as i call it) that can have many sections, each sections are to be separated by this special chars

[++++++++++++++++++++++++++++++]

and the title of each section will be inside this special curly braces

{{title here}}

each section can have as many text as required.

Ex:

template.txt


[++++++++++++++++++++++++++++++]
{{First}}

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.



[++++++++++++++++++++++++++++++]
{{Second}}

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

Using PHP, I want to read the template.txt file and create an array where the key of the array will be the title and the value will be the content.

For eg:


Array
(
    [First] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    [Second] => It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
)


index.php


<?php
$content = file_get_contents('template.txt');
$explode = explode('[++++++++++++++++++++++++++++++]', trim($content));
$final = array();

foreach($explode as $key => $val){
	preg_match('/{{(.+)}}/', $val, $matches);
	$variable = $matches[0];
	$title = $matches[1];
	$final[$title] = trim(str_replace($variable, '', $val));
}

array_shift($final);
print_r($final);
?>

Kindly go through the code and let me know / suggest me if this is fine or can be further optimized or made simple??

Many thanks in advance

You should use (.+?) to make it non greedy and prevent that in strings like

{{ replaceme }} and {{ measwell }}

The expression matches

“replaceme }} and {{ measwell”

Instead of

“replaceme” and “measwell”