How to regex negative number and dashes?

Member

by kadin , in category: Third Party Scripts , 5 months ago

How to regex negative number and dashes?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , 5 months 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.

Related Threads:

How to handle strings that contain negative numbers using regex?
How to optimize number regex in javascript?
How to check the phone number using regex?
How to limit input number using regex?
How to find a regex pattern after a specific number of characters?
How to detect phone number on multiline using regex?