How to match groups of strings that do not contain some substring

I need to match groups of strings that have as boundaries the sequence "__" char (two underscores)

eg:

hello __1the_re__ my name is __pe er33__

      

"1the_re" and "pe er33" must be matched

my problem is defining "a string that does not contain a character sequence"

/__((?!__).*)__/

      

I tried this but it doesn't work ...

Thank you!

+3


source to share


3 answers


Are you close:

/__((?!__).)*__/

      

works. The star must be outside the repeating group, so the scan is performed at each position, not immediately after the presenter __

.



Since this does not display the correct text (I am assuming you want the content between the double underscores to be captured), you probably want

/__((?:(?!__).)*)__/

      

+3


source


Within your grouping, you want to do one of the following:

  • Any character followed by any character, not _

    .
  • Any character is not _

Regex:

  /__(.[^_]|[^_])*__/

      

Like first matches first, it continues. To get a better match retrieval, add a no-capture flag and an internal match:

 /__((?:.[^_]|[^_])*)__/

      



Example:

$subject = 'hello __1the_re__ my name is __pe er33__';
$pattern = '/__((?:.[^_]|[^_])*)__/';
$r = preg_match_all($pattern, $subject, $match);
print_r($match[1]);

      

Output:

Array
(
    [0] => 1the_re
    [1] => pe er33
)

      

But it's obviously much easier to make the quantifier lazy:

/__(.+?)__/

      

+1


source


You can use an unwanted mark: "?".

/__((?!__).*?)__/g
// javascript:
>>> "hello __1the_re__ my name is __pe er33__".match(/__((?!__).*?)__/g)
["__1the_re__", "__pe er33__"]

      

0


source







All Articles