derstauner
derstauner

Reputation: 1796

regex string between comma or parenthese but not contains dot

my goal is to have a regex, witch captures the following:

F. e.:

For the substring excluding the boundaries I have this regex:

(?<=[\(\,])(.*?)(?=[\)\,])

It works fine.

Now I wanted to extend it to deny any strings containing dot like this:

(?<=[\(\,])((?!\.).*?)(?=[\)\,])

But it doesnt' filter out the strings with dot, f. e. this remains valid:

'(xxxx.yyyy)'

How should I adjust the regex?

Thanks.

Upvotes: 1

Views: 79

Answers (2)

Ryszard Czech
Ryszard Czech

Reputation: 18611

Use

(?<=[(,])[^(),.]*(?=[),])

See proof.

Explanation

NODE EXPLANATION
(?<= look behind to see if there is:
[(,] any character of: '(', ','
) end of look-behind
[^(),.]* any character except: '(', ')', ',', '.' (0 or more times (matching the most amount possible))
(?= look ahead to see if there is:
[),] any character of: ')', ','
) end of look-ahead

Upvotes: 0

anubhava
anubhava

Reputation: 785146

You may use this regex with a negated character class:

(?<=[(,])([^.]*?)(?=[),])

[^.] will match any character that is not a dot.

Also note that there is no need to escape characters like (, ). , etc inside a character class i.e. [...]

Upvotes: 2

Related Questions