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

I am trying to create a regex that does not match a word (only az) if the word has :

at the end, but it matches it otherwise. However, this word is in the middle of a larger regex , and so I (don't think) you can use negative lookbehind and metacharacter $

.

I tried this negative look instead:

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

      

but this test case

example:

      

just matches

exampl

      

instead of giving up.

+2


source to share


4 answers


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

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

      

ie: "match at least one az char, except that the following characters are 0 to n 'a-z' followed by ':'"

This will support larger regex:



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

      

will match the following line:

 Xeee Xrrr:Y XzzzY XfffZ

      

only "XyyyY"

+4


source


Try the following:



[a-z]\s

      

0


source


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

      

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

0


source


[a-d] + (?! [: A-d])

0


source







All Articles