Reputation: 8151
Don't know how to explain this, so an example:
xx_xx -> matches because of the underscore -> .*_.*
xx_t_xx -> dont want this to match as _t_ is an exception I want to ignore
xx_t_xx_xx -> matches as there is an underscore that is not part of the string _t_
xx_t_xx_t_xx -> no match
_t_ -> no match
_ -> match
_t__ -> match
So match underscores unless it is part of the string _t_
Can that be done with regex?
Upvotes: 0
Views: 60
Reputation: 2144
Finally Done, checked all your conditions. works fine Try this, it accepts any single character. This will definitely work for you
^([^_]|(_t_))*_([^_]|(_t_))*$
Upvotes: 1
Reputation: 12797
If your language supports negative lookbehind and negative lookahead, you can use (?<!_t)_(?!t_)
regex. Basically you search for _
not preceded by _t
and not followed by t_
Upvotes: 1
Reputation: 11116
okay
use this : ^[a-z]+_[a-z]+$
this will only match one underscore
i.e it will match xx_xx
but not xx_t_xx
try this in your console :
var str = "xx_xx";
var res = /^[a-z]+_[a-z]+$/.test(str);
console.log(res);
and yes one more thing . LEARN REGEX
its very helpful. you will like to start from regexone
Upvotes: 2