How to regex negative number and dashes?

Member

by kadin , in category: Third Party Scripts , a month ago

How to regex negative number and dashes?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , a month ago

@kadin 

To regex negative numbers and dashes, you can use the following regular expression pattern:

1
^[-]?d+$


Explanation:

  • ^ : asserts position at the start of the line
  • [-]? : matches an optional negative sign (-)
  • d+ : matches one or more digits (0-9)
  • $ : asserts position at the end of the line


This pattern will match both negative numbers (e.g. -1, -100) and positive numbers (e.g. 1, 100) with an optional negative sign at the beginning.