0

Basically, a comparison operator followed by (an optional space and) a number. Decimal points are allowed. This is what I got but it doesn't seem to work.

^(>|>=|<|<=|=|==|!=|<>)[.0-9]+$

So, examples that should match:

  • >9
  • <=10
  • != 20.0

etc

Examples that should NOT match:

  • >>2
  • <
  • =20.

etc

Any help appreciated.

Snowman
  • 2,465
  • 6
  • 21
  • 32

3 Answers3

2

Here you go:

(((<|>)=?)|(!=))\s?\d+(.\d+)?

http://regex101.com/r/pT6xK6/2

Cheers!

jdabrowski
  • 1,805
  • 14
  • 21
  • Works like a charm. Thanks a million. But you should include options for == and = signs. – Snowman Jul 03 '14 at 08:05
  • attention with `\d` that match `[0-9]` and other digit characters like the Eastern Arabic numerals. http://stackoverflow.com/questions/273141/regex-for-numbers-only – faby Jul 03 '14 at 08:09
1

this should work

^(>|>=|<|<=|=|==|!=|<>){1}\s?[0-9]+([.][0-9]+)?$

explanation

(>|>=|<|<=|=|==|!=|<>){1} <-- match one of this signs

[0-9]+<-- one or more numbers

([.][0-9]+)? <-- zero or more occurence of . plus numbers

I don't use \d because match [0-9] and other digit characters like the Eastern Arabic numerals

faby
  • 7,394
  • 3
  • 27
  • 44
0

Try the below regex to match a comparison operator followed by (an optional space and) a number,

^(?:>|>=|<|<=|=|==|!=|<>)\s?[0-9]+[.]?(?:[0-9]+)?$

DEMO

You are so close. You need to make the capturing group into non-capturing group and also you have to add the pattern for optional space ie,\s?

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274