Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match word boundary string with parentheses

Tags:

regex

php

I am trying to create a regex that checks if tag is inside the text, but there are couple tags (from multiple thousands, don't ask me why, SEO expert told my client its good) which end with parentheses.

The regex works great for normal tags, but plain fails on parentheses, as the match has to be exact, so I am forced to use word boundary. Is there a way to allow this?

Here is original regex I used:

https://regex101.com/r/wN9jO8/1

This is what I tried (yes, I am not good with regexes, but I tried googling and cold not find anything really useful):

https://regex101.com/r/wN9jO8/2

I also tried modifying word boundary, but it did not work correctly (always matched one letter of string in front and after the tag).

Basically, for the tag text (jadad):

lipsum is a dummy text (jadad) alsdasldk. // match
lipsum is a dummy text (jadad). // match
lipsum is a dummy text (jadad) // match
lipsum is a dummy (text (jadad)) // match

lipsum is a dummy text (jadad // should not match
lipsum is a dummy text jadad) // should not match
lipsum is a dummy text (jadad)asd // should not match

The main problem is, it has to work perfectly fine for tags with parenthesis and without them, ideally easily editable to support more weird characters in tags ([ or > or tag ending with . or ? or !).

I am really lost right now. If you need any more info, just comment and I will try to add it in.

Thanks for help.

like image 497
MiChAeLoKGB Avatar asked Dec 18 '22 17:12

MiChAeLoKGB


1 Answers

You can use a negative lookahead (?!\w) (means next position doesn't have a word char). Note that you cannot use \b as \b cannot assert after ) which is considered a non-word character:

\btext \(jadad\)(?!\w)

Updated Regex Demo

like image 94
anubhava Avatar answered Dec 21 '22 06:12

anubhava