tags:

views:

62

answers:

3
+1  Q: 

regular expression

(.[^_]+) 

Matches correctly when there is no underscore, how can I modify this regex to match when there is no underscore only before a question mark ?

ie. ignore any underscores after ?

+2  A: 

This will only allow underscores after the question mark:

(.[^_]*(\?.*)?)

Ignacio Vazquez-Abrams
A: 

.[^_]*?\?.*

Anything except for underscore zero or more times, lazy quantifier (the shortest match), followed by a question mark. Another option:

.[^_\?]*\?.*

Dmitry
A: 

Put the question mark itself into your negated character class:

(.[^_?]+)

This will match all characters until it’s either an underscore or a question mark.

Gumbo