5

How to do we compose a regex that says "the matches fails if there's a dash somewhere in the middle"

I have several lines that are composed as 3958.3r - 5v and some are without the dash for example: 3958.3v4r. I am able to get the ones with the dash, but not only the ones without the dash

LiquidSnake
  • 375
  • 1
  • 4
  • 16
  • So, you need both? Or want only without dash? – Hardik Shah Jul 09 '18 at 13:06
  • You want to match any string but [*a **string containing specific character***](https://stackoverflow.com/a/37988661/3832970): `^[^-]*$` or `^[^-]+$` (if empty strings are not allowed). – Wiktor Stribiżew Jul 09 '18 at 13:06
  • @HardikShah I need to get first the ones with the dash only, and second the ones without the dash to store them somewhere for example – LiquidSnake Jul 09 '18 at 13:10

1 Answers1

7

This can be accomplished with a character class negation. ^ at the beginning of a character class simply negates the character class. If you then only have the character -, then you create a character class that matches anything but -.

^[^\-]+$

According to what you have said, you need to put the ^ (start of string) at the front of your pattern, and escape the . or else you will match not only the . after 395[0-9], but any character at all like 3950z.

^395[0-9]\.[^-]+$
Evan
  • 2,120
  • 1
  • 15
  • 20