开发者

Regex: How to not match the last character of a word?

I am tr开发者_Go百科ying to create a regex that does not match a word (a-z only) if the word has a : on the end but otherwise matches it. However, this word is in the middle of a larger regex and so I (don't think) you can use a negative lookbehind and the $ metacharacter.

I tried this negative lookahead instead:

([a-z]+)(?!:)

but this test case

example:

just matches to

exampl

instead of failing.


If you are using a negative lookahead, you could put it at the beginning:

(?![a-z]*:)[a-z]+

i.e: "match at least one a-z char, except if the following chars are 0 to n 'a-z' followed by a ':'"

That would support a larger regex:

 X(?![a-z]*:)[a-z]+Y

would match in the following string:

 Xeee Xrrr:Y XzzzY XfffZ

only 'XzzzY'


Try this:

[a-z]\s


([a-z]+\b)(?!:)

asserts a word boundary at the end of the match and thus will fail "exampl"


[a-z]+(?![:a-z])

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜