6

My use case is as follows: I would like to find all occurrences of something similar to this /name.action, but where the last part is not .action eg:

  • name.actoin - should match
  • name.action - should not match
  • nameaction - should not match

I have this:
/\w+.\w*
to match two words separated by a dot, but I don't know how to add 'and do not match .action'.

cherouvim
  • 31,301
  • 15
  • 102
  • 148
Ula Krukar
  • 11,859
  • 20
  • 49
  • 65

5 Answers5

8

Firstly, you need to escape your . character as that's taken as any character in Regex.

Secondly, you need to add in a Match if suffix is not present group - signified by the (?!) syntax.

You may also want to put a circumflex ^ to signify the start of a new line and change your * (any repetitions) to a + (one or more repititions).

^/\w+\.(?!action)\w+ is the finished Regex.

Daniel May
  • 8,096
  • 1
  • 31
  • 43
  • In most regex implementations, the `.` does not match `\r` and `\n`. – Bart Kiers Dec 10 '09 at 15:26
  • Whether `\w` applies to ASCII alone depends on the programming language used, and sometimes to regex compilation flags — e.g., in some, you need a `/u` or `(?u)`, while in others (like Java) even that is insufficient to get include Unicode alphanums. – tchrist Nov 06 '10 at 18:01
3
^\w+\.(?!action)\w*
ʞɔıu
  • 45,214
  • 31
  • 101
  • 144
0

You need to escape the dot character.

mfabish
  • 190
  • 1
0
\w+\.(?!action).*

Note the trailing .* Not sure what you want to do after the action text.

See also Regular expression to match string not containing a word?

Community
  • 1
  • 1
Jesse Vogt
  • 15,601
  • 14
  • 56
  • 71
0

You'll need to use a zero-width negative lookahead assertion. This will let you look ahead in the string, and match based on the negation of a word.

So the regex you'd need (including the escaped . character) would look something like:

/name\.(?!action)/
Dave Challis
  • 3,186
  • 2
  • 32
  • 61