Page 1 of 1

Regex Question. How to extract number from "8iedgf-456.45asd"f

Posted: 24 Jan 2024, 10:04
by Spitzi
Hi there.

Regex is quite hard to understand...

I have this code to give me a number embeded in a string as mentioned in the title

Code: Select all

p := RegExMatch(imageText, "[\d.]+", &m) 
MsgBox(m[] " found at position " p)
It yields positive values. How do I have to adapt the expression to include the minus sign?

Thanks

Re: Regex Question. How to extract number from "8iedgf-456.45asd"f

Posted: 24 Jan 2024, 11:07
by sofista
In order to match the minus sign, include it at the end of the defined class: [\d.-]+ .

Re: Regex Question. How to extract number from "8iedgf-456.45asd"f

Posted: 24 Jan 2024, 12:14
by Spitzi
Hi @sofista , thank you. But this expression will also match with, e.g. -67-04.3.0 in e.g. asdf-67-04.3.0ghkj
I would like to extract the first regular number from the string, in this case -67. How would I do that?

Re: Regex Question. How to extract number from "8iedgf-456.45asd"f

Posted: 24 Jan 2024, 12:37
by andymbody
Spitzi wrote:
24 Jan 2024, 12:14
I would like to extract the first regular number from the string
Here is your first example.
8iedgf-456.45asd"f

The first regular number in this string will be 8. You have also mentioned that you would like -456. And then -67 in the last example. There is a wide variation between your haystack strings in each example vs the number you wish to extract.

Do you have a list of at least 10 example strings so others can see if there is any common pattern to design an effective needle?

Or maybe a source string that represents the actual data to be extracted from?

Re: Regex Question. How to extract number from "8iedgf-456.45asd"f

Posted: 24 Jan 2024, 13:17
by sofista
I second @andymbody requests.