Solved

Regular expression

Posted on 2006-07-21
13
199 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl count the hash for print 4 156
Expect & sshpass package for Solaris 10 x86 15 852
stftime format 4 51
Perl modules on linux ec2 3 85
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now