Solved

Regular expression

Posted on 2006-07-21
13
202 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 158
Perl script to parse log and delete the file 17 178
Perl for loop for 2000 ms 7 105
Perl output collect 7 67
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

829 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