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

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

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

Post by Spitzi » 24 Jan 2024, 10:04

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

sofista
Posts: 654
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

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

Post by sofista » 24 Jan 2024, 11:07

In order to match the minus sign, include it at the end of the defined class: [\d.-]+ .

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

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

Post by Spitzi » 24 Jan 2024, 12:14

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?

User avatar
andymbody
Posts: 939
Joined: 02 Jul 2017, 23:47

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

Post by andymbody » 24 Jan 2024, 12:37

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?

sofista
Posts: 654
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

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

Post by sofista » 24 Jan 2024, 13:17

I second @andymbody requests.

Post Reply

Return to “Ask for Help (v2)”