Regular expression

I have the following String:

$Str="abc{{#if:{{{1|}}}|List of Spanish monarchs | MISSING ARTICLES
}}{{#if:{{{2|}}}|{{#if:{{{3|}}}|, | and }} Kings of Spain family tree }}{{#if:{{{3|}}}|{{#if:{{{4|}}}|, |, and }} }}{{#if:{{{4|}}}|{{#if:{{{5|}}}|, |, and }} }}{{#if:{{{5|}}}|{{#if:{{{6|}}}|, |, and }} }}{{#if:{{{6|}}}|{{#if:{{{7|}}}|, |, and }} }}{{#if:{{{7|}}}|{{#if:{{{8|}}}|, |, and }} }}{{#if:{{{8|}}}|{{#if:{{{9|}}}|, |, and }} }}{{#if:{{{9|}}}|{{#if:{{{10|}}}|, |, and }} }}{{#if:{{{10|}}}|{{#if:{{{11|}}}|, |, and }} }}{{#if:{{{11|}}}|{{#if:{{{12|}}}|, |, and }} }}{{#if:{{{12|}}}|{{#if:{{{13|}}}|, |, and }} }}{{#if:{{{13|}}}|{{#if:{{{14|}}}|, |, and }} }}{{#if:{{{14|}}}|{{#if:{{{15|}}}|, |, and }} }}{{#if:{{{15|}}}|, and }}{{#if:{{{16|}}}|…Template:See also MAXIMUM 15 ARTICLES ALLOWED}}def";

I would to replace all what between {{.....}} by nothing to get $str="abcdef"; using one time expression
I tried to replace '\{\{.*?\}\}' by nothing but it give not appropriate result
mmbabaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Harisha M GConnect With a Mentor Commented:
mmbaba, I have tested it myself, and it works..

http://mgharish.5gigs.com/test.php

The code for that page is:

<html>
<head>
<title></title>
</head>

<body bgcolor="#FFFFFF">
<?php
    if(isset($_POST['str']))
    {
         echo preg_replace("/{{.*}}/s","",$_POST['str']);
    }
    else
    {
?>
         <form method="POST" action="<?$_SERVER['PHP_SELF']?>">
              <textarea name="str"></textarea>
              <input type="submit" name="submit" value="Check">
         </form>
<?
    }
?>
</body>
</html>
0
 
Harisha M GCommented:
Hi, try this:

$Str =~ s/{{.*}}//g

---
Harish
0
 
Harisha M GCommented:
g is not required..
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
ozoCommented:
If there are newlines in that string then /s is required
$Str =~ s/{{.*}}//s;
0
 
mmbabaAuthor Commented:
You do not forget that the string contains "{" and "}" chars and those are also have meaning in the Reg.Exp. so please try any suggestion before submission, are you sure that it is working !

your suggestion should written as following:
 $Str =~ s/\{\{.*\}\}//gs;
and I need it also in the PHP and the same suggestion should written as following:

$search = array("'\{\{.*\}\}'");
$replace= array("");
 $Str= preg_replace ($search, $replace, $Str);

the both give the result:
$Str = "abc|List of Spanish monarchs | MISSING ARTICLES }}def";
the right exp. should take in count the first "{{" after "abc" and the last "}}" before "def"
0
 
ozoCommented:
If there is a newline after "MISSING ARTICLES\n" then
$Str =~ s/{{.*}}//g
without the /s will prodice
abc|List of Spanish monarchs | MISSING ARTICLES
}}def
but
$Str =~ s/{{.*}}//s
would produce
abcdef
0
 
Harisha M GCommented:
<?php

$Str="abc{{#if:{{{1|}}}|List of Spanish monarchs | MISSING ARTICLES
}}{{#if:{{{2|}}}|{{#if:{{{3|}}}|, | and }} Kings of Spain family tree }}{{#if:{{{3|}}}|{{#if:{{{4|}}}|, |, and }} }}{{#if:{{{4|}}}|{{#if:{{{5|}}}|, |, and }} }}{{#if:{{{5|}}}|{{#if:{{{6|}}}|, |, and }} }}{{#if:{{{6|}}}|{{#if:{{{7|}}}|, |, and }} }}{{#if:{{{7|}}}|{{#if:{{{8|}}}|, |, and }} }}{{#if:{{{8|}}}|{{#if:{{{9|}}}|, |, and }} }}{{#if:{{{9|}}}|{{#if:{{{10|}}}|, |, and }} }}{{#if:{{{10|}}}|{{#if:{{{11|}}}|, |, and }} }}{{#if:{{{11|}}}|{{#if:{{{12|}}}|, |, and }} }}{{#if:{{{12|}}}|{{#if:{{{13|}}}|, |, and }} }}{{#if:{{{13|}}}|{{#if:{{{14|}}}|, |, and }} }}{{#if:{{{14|}}}|{{#if:{{{15|}}}|, |, and }} }}{{#if:{{{15|}}}|, and }}{{#if:{{{16|}}}|…Template:See also MAXIMUM 15 ARTICLES ALLOWED}}def";

echo preg_replace("/{{.*}}/s","",$Str);

?>
0
 
Harisha M GCommented:
>> You do not forget that the string contains "{" and "}" chars and those are also have meaning in the Reg.Exp.

I am not aware of that.. what do they represent ?
0
 
ozoCommented:
x{4} means match x 4 times
x{4,} means match x at least 4 times
x{4,10} means match x at least 4 but not more than 10 times
but in the context of
s/{{.*}}//s
in Perl it just matches a literal {
I'm not sure about php
0
 
Harisha M GCommented:
Oh.. that :)

Same in PHP..

In PHP preg_match uses PCRE (Perl Compatible Regular Expressions)
ereg_match uses standard RegExes.. but I don't know that standard.. (it uses something like [[:alpha:]] etc
0
 
mmbabaAuthor Commented:
echo preg_replace('/{{.*}}/s',"",$Str);

return

abc|List of Spanish monarchs | MISSING ARTICLES }}def

to do test yourself try by copy and paste the following
<?
$Str="abc{{#if:{{{1|}}}|List of Spanish monarchs | MISSING ARTICLES
}}{{#if:{{{2|}}}|{{#if:{{{3|}}}|, | and }} Kings of Spain family tree }}{{#if:{{{3|}}}|{{#if:{{{4|}}}|, |, and }} }}{{#if:{{{4|}}}|{{#if:{{{5|}}}|, |, and }} }}{{#if:{{{5|}}}|{{#if:{{{6|}}}|, |, and }} }}{{#if:{{{6|}}}|{{#if:{{{7|}}}|, |, and }} }}{{#if:{{{7|}}}|{{#if:{{{8|}}}|, |, and }} }}{{#if:{{{8|}}}|{{#if:{{{9|}}}|, |, and }} }}{{#if:{{{9|}}}|{{#if:{{{10|}}}|, |, and }} }}{{#if:{{{10|}}}|{{#if:{{{11|}}}|, |, and }} }}{{#if:{{{11|}}}|{{#if:{{{12|}}}|, |, and }} }}{{#if:{{{12|}}}|{{#if:{{{13|}}}|, |, and }} }}{{#if:{{{13|}}}|{{#if:{{{14|}}}|, |, and }} }}{{#if:{{{14|}}}|{{#if:{{{15|}}}|, |, and }} }}{{#if:{{{15|}}}|, and }}{{#if:{{{16|}}}|…Template:See also MAXIMUM 15 ARTICLES ALLOWED}}def";

echo preg_replace('/{{.*}}/s',"",$Str); // replaced " by ' to give result
?>
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
While the above solutions work for the example string provided, I think mmbaba is maybe after a little bit more.

For example, the above solutions when applied to something like "abc{zz{}zz}def{y{yy}y}ghi" would give "abcghi" where I suspect mmbaba is after it returning "abcdefghi".

If the above is correct, then you would need to use a PHP line in the above examples like the following

echo preg_replace("/{(([^{}]|(?R))*)}/","",$Str);

Basically, the (?R) gives you recursion, so the pattern that you look for is: match a braced expression, which is a '{' followed by (any number of (non-brace characters or other braced expressions)) followed by '}'

Hope this helps...
0
 
mmbabaAuthor Commented:
OK mgh_mgharish ,

there is a new line after "MISSING ARTICLES" for that /s not work , I tried by doing the replacement it (\n) by noting  before apply it and it work as expexted.

the code is :

$search  = array ("'\n'",
                  "{{.*}}"
                  );
$replace = array ("",
                  ""
                  );
 $Str= preg_replace ($search, $replace, $Str);
0
All Courses

From novice to tech pro — start learning today.