[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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
0
mmbaba
Asked:
mmbaba
  • 6
  • 3
  • 3
  • +1
1 Solution
 
Harisha M GCommented:
Hi, try this:

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

---
Harish
0
 
Harisha M GCommented:
g is not required..
0
 
ozoCommented:
If there are newlines in that string then /s is required
$Str =~ s/{{.*}}//s;
0
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!

 
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
 
Harisha M GCommented:
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now