tags:

views:

129

answers:

5

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'.

A: 

You need to escape the dot character.

mfabish
That's just a (small) part of the problem.
Bart Kiers
+2  A: 
^\w+\.(?!action)\w*
ʞɔıu
+3  A: 

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
In most regex implementations, the `.` does not match `\r` and `\n`.
Bart Kiers
A: 
\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?

Jesse
A: 

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