parse a string with regex delimiter

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ernestas
Posts: 33
Joined: 16 Apr 2018, 02:07

parse a string with regex delimiter

28 Jan 2019, 03:07

Hello!

It's been a while since I raised a question here! It means I progress :D
But here I have a problem with parsing a string with regex delimiter. I would be grateful if you could help me with this one!!!

Code: Select all

string = jk12345jk12345jk12345

test:= []
Loop, parse, string , "([a-zA-Z]{2})(\d{5})"
{
   test.insert(A_loopfield)
}

for index, element in test
   MsgBox % "Element number " . index . " is " . element
And I want to have the string to look like this (with the spaces between) - string = jk12345 jk12345 jk12345
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: parse a string with regex delimiter

28 Jan 2019, 03:32

Ernestas wrote:
28 Jan 2019, 03:07
Hello!

It's been a while since I raised a question here! It means I progress :D
But here I have a problem with parsing a string with regex delimiter. I would be grateful if you could help me with this one!!!

Code: Select all

string = jk12345jk12345jk12345

test:= []
Loop, parse, string , "([a-zA-Z]{2})(\d{5})"
{
   test.insert(A_loopfield)
}

for index, element in test
   MsgBox % "Element number " . index . " is " . element
And I want to have the string to look like this (with the spaces between) - string = jk12345 jk12345 jk12345
It's hard to understand what you want, relative to your request. If you want jk12345 jk12345 jk12345, then do the below.

Code: Select all


string = jk12345jk12345jk12345

Loop, parse, string , "5"
{
	pre .= A_Loopfield . A_Space
}
string :=""
string := pre

msgbox % string
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: parse a string with regex delimiter

28 Jan 2019, 03:38

Code: Select all

s := "jk12345jk12345jk12345"
p := "([a-zA-Z]{2})(\d{5})"
MsgBox % RegExReplace(s, "(?<=" p ")(?=(" p ")+$)", " ") 
Last edited by IMEime on 28 Jan 2019, 04:02, edited 1 time in total.
Ernestas
Posts: 33
Joined: 16 Apr 2018, 02:07

Re: parse a string with regex delimiter

28 Jan 2019, 03:56

Thank you for your replies!
I see that I might have described my problem in a wrong way. The thing is that the variation of the string can be different every time.
for example it could be:

string = jk12345jk12345jk12345 or string = kt25678eg78546yh25874 or even it could be like this string = sd12354fg14582gh25468jj54582rr45821lk45896qq7854.

I mean it is always two first letters and 5 digits. and it could be a different amount of this combination. Any other thoughts?

Really appreciate!!!
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: parse a string with regex delimiter

28 Jan 2019, 04:01

I hate this kind of people
They just asking
Not try even a line of code with its own fingers.

Code: Select all

s =
(
jk12345jk12345jk12345 
kt25678eg78546yh25874
sd12354fg14582gh25468jj54582rr45821lk45896
)
p := "([a-zA-Z]{2})(\d{5})"
Loop, Parse, s, `n, `r
	r .= RegExReplace(A_LoopField, "(?<=" p ")(?=(" p ")+$)", " ")  "`n"
MsgBox % r
Ernestas
Posts: 33
Joined: 16 Apr 2018, 02:07

Re: parse a string with regex delimiter

28 Jan 2019, 05:22

IMEime wrote:
28 Jan 2019, 04:01
I hate this kind of people
They just asking
Not try even a line of code with its own fingers.

Code: Select all

s =
(
jk12345jk12345jk12345 
kt25678eg78546yh25874
sd12354fg14582gh25468jj54582rr45821lk45896
)
p := "([a-zA-Z]{2})(\d{5})"
Loop, Parse, s, `n, `r
	r .= RegExReplace(A_LoopField, "(?<=" p ")(?=(" p ")+$)", " ")  "`n"
MsgBox % r
Thank you it!

I will dig deeper in parsing the strings because it is a bit confusing for me. Thank you again! :)
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: parse a string with regex delimiter

28 Jan 2019, 05:25

it is just a sample of mine

you have to try/test it
No try no get

Good Luck To You
Last edited by IMEime on 28 Jan 2019, 06:42, edited 1 time in total.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: parse a string with regex delimiter

28 Jan 2019, 06:09

Ernestas wrote:
28 Jan 2019, 05:22
Thank you it!

I will dig deeper in parsing the strings because it is a bit confusing for me. Thank you again! :)
It's good that you will commit to studying. You will also learn there are various ways to do things, which is one of the great features of programming.

Here is an alternative method that you can also try. It will divide the string on the 5th character and add a space.

Code: Select all

string = sd12354fg14582gh25468jj54582rr45821lk45896qq7854

msgbox % string

no = 0
Loop, Parse, string
{
	no++
	result .= A_LoopField
	If (no = 5)
	{
	result .= A_Space
	no :=""
	}
}
msgbox % result
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: parse a string with regex delimiter

28 Jan 2019, 12:53

Code: Select all

string = sd12354fg14582gh25468jj54582rr45821lk45896qq7854
MsgBox % string := Trim(RegExReplace(string , "[a-zA-Z]+\d+", "$0 "), " ")

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Rohwedder, Tech Stuff and 354 guests