2

Suppose I have the following text:

__This_is__ a __test__

Using two underscores for denoting italics. So I expect This_is and test to be italicized. The logic dictates that any text between two consecutive double underscores should be italicized, including any other number of underscores that may be there. I've got:

__([^_]+)__

What is the equivalent of "not two consecutive underscores" in group 1? Thanks.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
qsmith
  • 23
  • 1
  • 3

2 Answers2

3

An option would be to match two underscores:

__

Then make a negative look ahead to see if theres no two underscores ahead of the current position:

__(?!__)

if that is not the case, match any character:

__(?!__). 

and repeat the previous one or more times:

__((?!__).)+

and finally match another two underscores:

__((?!__).)+__

which is the final solution.

A little demo:

<?php
$text = '__This_is__ a __test__';
preg_match_all('/__(?:(?!__).)+__/', $text, $matches);
print_r($matches);
?>

produces:

Array
(
    [0] => Array
        (
            [0] => __This_is__
            [1] => __test__
        )

)

as can be seen on Ideone.

EDIT

Note that I used a non-capturing group in my demo, otherwise the output would have looked like this:

Array
(
    [0] => Array
        (
            [0] => __This_is__
            [1] => __test__
        )

    [1] => Array
        (
            [0] => s
            [1] => t
        )

)

i.e. the last character matched by ((?!__).) would have been captured in group 1.

More about groups, see: http://www.regular-expressions.info/brackets.html

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Thanks! But it seems the code differs from the deduced regex. What is the extra `?:` in `__(?:(?!__).)+__` for? – qsmith Feb 02 '11 at 13:31
  • `(?:...)` is a non-capturing group whereas `(...)` captures the contents for later re-use. Since you're not reusing the repeated element, you can use the former construct and gain a little efficiency. – Tim Pietzcker Feb 02 '11 at 13:33
  • @qsmith, ah, I see @Tim beat me to the punch. Anyway, see my EDIT. – Bart Kiers Feb 02 '11 at 13:37
1
$text = '__This_is__ a __test__';
preg_match_all('/(__([\w]+)__)/', $text, $matches);
print_r($matches);

http://ideone.com/uHJCC

dynamic
  • 46,985
  • 55
  • 154
  • 231