How would you combine a "negate" wildcard [^] with another wild card?

I have the string "Programming. Is! Amazing.", and I want to learn how I can write a regex pattern using the wild cards \s to ignore the spaces and \W to match only the . and !.

[^\s]\W

The idea with the pattern is to negate white spaces with [^\s] and match . and ! by using \W., so that the result becomes .!.

The pattern is obviously not correct, but I would like to learn if there is a syntax that could give me the desired effect by basically combining [^] with another wildcard.

>Solution :

You can use so called "non-consuming" expression ("(?=*)"):

(?=[^\s])\W

Example:

https://regexr.com/7ffrt

Leave a Reply