Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

What is the RegEx for finding a semi-color between brackets? WordPress

  • SOLVED

Not really a WordPress expression, but surely you PHP geniuses can help. Assume I have text like this:

Hello, how are you? Nice day yes? [[ secret_code(); ]] Jolly, fun ride?

or:

Hello, how are you? Nice day yes? [[ secret_code() ]] Jolly, fun ride?

or:

In 1986 IBM recorded the largest profit ever recorded by any corporation in the the history of the world. Nothing would ever be able to compete with IBM. The media was in a swoon about how amazing IBM was. But IBM was already losing ground in the PC market, and they were losing ground in electronics to the Japanese. In [[ find_date_for_quote(); ]] IBM was struggling to avoid bankruptcy.

I need to remove every semi-colon that appears between the double square brackets [[ ]].

What is the regular expression for this?

This will be in a script that is looping hundreds of small text snippets like the ones above.

Answers (4)

2010-11-26

Larrie Bernd Rocha answers:


$new_string = preg_replace("/(\[\[) (\w+)(\(\))(\;) (\]\])/", "$1 $2$3 $5", $string , -1);


Larrie Bernd Rocha comments:

$new_string = preg_replace("/(\[\[) (.*)(;) (\]\])/", "$1 $2 $4",$your_string);


Larrie Bernd Rocha comments:

Hi Lawrence,

Just use this. Shortest code and effective. Already tried this and it works well.

$new_string = preg_replace("/(\[\[) (\w+)(\(\))(\;) (\]\])/", "$1 $2$3 $5", $string , -1);

Larrie


Larrie Bernd Rocha comments:

Mine is just a one-line implementation utilizing PHPs preg_replace function. It will search for ALL matches and remove the semicolon. No need to create new functions. This one is the best fix for this.

$new_string = preg_replace("/(\[\[) (\w+)(\(\))(\;) (\]\])/", "$1 $2$3 $5", $string , -1);

2010-11-26

rilwis answers:

These methods above let you replace <strong>one</strong> semicolon only. Here is the code to remove all semicolons:

function rw_remove_semicolons($match) {
$s = $match[0];
return str_replace(';', '', $s);
}

// test

$s = "In 1986 IBM recorded the largest profit ever recorded by any corporation in the the history of the world. Nothing would ever be able to compete with IBM. The media was in a swoon about how amazing IBM was. But IBM was already losing ground in the PC market, and they were losing ground in electronics to the Japanese. In [[ find_date_for_quote(); ]] IBM was struggling to avoid bankruptcy.";

// usage

$s = preg_replace_callback('#\[\[.*?\]\]#', 'rw_remove_semicolons', $s);

echo $s;


rilwis comments:

I'm very interested in given solutions. And in this race, I tried to make my 1st answer better and I found useful "e" modifier that allow us to reduce code (and make it easy to read). Here it is:

$s = "In 1986 IBM recorded the largest profit ever recorded by any corporation in the the history of the world. In [[ find_date_for_quote(); find_date_for_quote(); ]] IBM was struggling to avoid bankruptcy.";

$s = preg_replace('#\[\[.*?\]\]#e', "str_replace(';', '', '$0')", $s);

echo $s;

2010-11-25

Ehthisham tk answers:

ex:
From : [123]some text [caught] more text [123123] sq() []

you get :
[123]
[caught]
[123123]
[]

using this reg expression: <strong> \[[^\]|^\[]*\]</strong>
the following will match ()
\(([^\}]+)\)

2010-11-26

idt answers:

Hi Lawrence,

Just replace the match for this:
[;]+(?=[^\[\[]*]])

Edit: removed the double quote on my first answer to match all occurrences of ";" inside the [[]]


idt comments:

Tested it here: http://www.regextester.com/ and was able to find ";" for the both:

<blockquote>Hello, how are you? Nice day yes? [[ secret_code(); ]] Jolly, fun ride?</blockquote> and <blockquote>In 1986 IBM recorded the largest profit ever recorded by any corporation in the the history of the world. Nothing would ever be able to compete with IBM. The media was in a swoon about how amazing IBM was. But IBM was already losing ground in the PC market, and they were losing ground in electronics to the Japanese. In [[ find_date_for_quote(); ]] IBM was struggling to avoid bankruptcy.</blockquote>