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,725
  • 15
  • 104
  • 153
Ula Krukar
  • 12,549
  • 20
  • 51
  • 65

5 Answers5

9

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,156
  • 1
  • 33
  • 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
  • 47,148
  • 35
  • 106
  • 149
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
  • 16,229
  • 16
  • 59
  • 72
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,525
  • 2
  • 37
  • 65