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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With