Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help With Particular Regular Expression - Not Containing Some String

How do I say, in regular expressions:

Any portion of a string beginning with a capital letter, containing at least one space character, not containing the string
" _ " (space underscore space), and ending with the string "!!!" (without the quotes)?

I am having trouble with the "not containing" part.

Here is what I have so far:

[A-Z].* .*!!!

How do I modify this to also specify "Not containing ' _ '"?

It does not need to be the specific string " _ ". How can I say "not containing" ANY string? For instance not containing "dog"?

Edit: I'd like the solution to be compatible with Php's "preg_replace"

Edit: Examples:

Examples for " _ ":

Abc xyz!!! <---Matches

Hello World!!! <---Matches

Has _ Space Underscore Space!!! <--- Does Not Match

Examples for "dog":

What a dog!!! <--- Does not match, (contains "dog")

Hello World!!! <--- Matches

like image 598
Joshua Avatar asked Nov 05 '22 07:11

Joshua


2 Answers

The x(?!y) expression matches x only if it is not immediately followed by y. So, this seems to be the thing you want:

[A-Z](?!%s)(.(?!%s))* (.(?!%s))*!!!

Where %s is your forbidden string.

like image 71
atomizer Avatar answered Nov 09 '22 07:11

atomizer


Any possible regex for this would be probably much more complicated than two regexes. One like yours: [A-Z].* .*!!! and the second applied on matched strings and checking whether _ is contained.

like image 34
eumiro Avatar answered Nov 09 '22 07:11

eumiro