Solved

Regular expression

Posted on 2006-07-21
13
205 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:mmbaba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17158389
Hi, try this:

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

---
Harish
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17158396
g is not required..
0
 
LVL 84

Expert Comment

by:ozo
ID: 17158562
If there are newlines in that string then /s is required
$Str =~ s/{{.*}}//s;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mmbaba
ID: 17158646
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
 
LVL 84

Expert Comment

by:ozo
ID: 17158661
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17160867
<?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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17160871
>> 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
 
LVL 84

Expert Comment

by:ozo
ID: 17161252
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17161261
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
 

Author Comment

by:mmbaba
ID: 17162505
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
 
LVL 37

Accepted Solution

by:
Harisha M G earned 100 total points
ID: 17162735
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
 
LVL 35

Expert Comment

by:mccarl
ID: 17163643
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
 

Author Comment

by:mmbaba
ID: 17167781
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question