I know this stuff has been talked about a lot, but I’m having a problem trying to match the following…
Example input: “test test 310-315”
I need a regex expression that recognizes a number followed by a dash, and returns 310. How do I include the dash in the regex expression though. So the final match result would be: “310”.
Thanks a lot – kcross
EDIT: Also, how would I do the same thing but with the dash preceding, but also take into account that the number following the dash could be a negative number… didnt think of this one when I wrote the question immediately. for example: “test test 310–315” returns -315 and “test 310-315” returns 315.
\d+– Looks for one or more digits(?=\-)– Makes sure it is followed by a dashThe
@just eliminates the need to escape the backslashes to keep the compiler happy.Also, you may want this instead:
This will check for a one or more numbers, followed by a dash, followed by one or more numbers, but only match the first set.
In response to your comment, here’s a regex that will check for a number following a
-, while accounting for potential negative (-) numbers:(?<=\-)– Negative lookbehind which will check and make sure there is a preceding-\-?– Checks for either zero or one dashes\d+– One or more digits