Regex - help simplify function, define problems Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DanRim
Posts: 153
Joined: 20 Jul 2018, 15:16

Regex - help simplify function, define problems

Post by DanRim » 13 Oct 2020, 15:34

Hello,
This function helped me to build @boiler and @mikeyww. Many thanks for help and support.
I have updated this function and I think I am almost finished with this part.

Function works and does what I need, but I just wondering if you could help me simplify function or make more dynamic if possible or maybe just give me notes/comments where could be problem in the future.

The goal is to find in the string numbers which are made from 7 digits, string - FORNYELSECODE and single digit. So all single digits of 7 or with additional strings Fornyelsecode and single digit is good. single digits will be validated later.

Just idea:
I thought I would use RegExReplace, but I could not manage to write syntax which could clear "Polisenr Modul Selskap", any doubledigits, string "HAR". I thought it will move every part of string to the left position, it means if we have 08 9911118 00 HAR FORNYELSECODE 3 after replacement will be 9911118 FORNYELSECODE 3 and 7 digits from m2 will move to m1 and so on, and I will have clear positions which I can manipulate. But this was just idea. did not had chance to check.


Code: Select all


^!e::
str = 
(								
Polisenr Modul Selskap
1234567 06 07
08 9911118 00 HAR FORNYELSECODE 3
Polisenr Modul Selskap
2345678

09 7654321 00 HAR FORNYELSECODE 7
04 1122334 00 HAR FORNYELSECODE 8
0125878
)

loop, Parse, str, `n, `r
{
	RegExMatch(A_LoopField, "\s*(\w+)*\s*(\w+)*\s*(\w+)*\s*(\w+)*\s*(\w+)*\s*(\w+)*", m)   	
	
	if (m1 = "Polisenr" AND m2 = "Modul" AND m3 = "Selskap") {
		continue
	}	
	if (m1 = 08 OR m1 = 09 OR m1 = 04) {
		m1 = %m2%
		MsgBox, %m1% %m5% %m6%; example: 9911118 FORNYELSECODE 3 or 0125878

		if(m6 = 8 OR m6 = 7 OR m6 = 3){
			MsgBox, % m6
		}
	}
	;~ MsgBox, % "part 1: " m1 "`npart 2: " m2 "`npart 3: " m3 "`npart 4: " m4 "`npart 5: " m5 "`npart 6: " m6
}
return



Attachments
single3.JPG
single3.JPG (97.7 KiB) Viewed 273 times

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Regex - help simplify function, define problems

Post by mikeyww » 13 Oct 2020, 16:13

Code: Select all

str =
(
Polisenr Modul Selskap
1234567 06 07
08 9911118 00 HAR FORNYELSECODE 3
Polisenr Modul Selskap
2345678

09 7654321 00 HAR FORNYELSECODE 7
04 1122334 00 HAR FORNYELSECODE 8
0125878
)

Loop, Parse, str, `n, `r
{
 RegExMatch(A_LoopField, "(\d\d )?(\d{7})( \d\d HAR)?( FORNYELSECODE \d)?", m)
 If StrLen(match := m2 m4)
  MsgBox, 64, Match on line #%A_Index%, %match%
}
Return

DanRim
Posts: 153
Joined: 20 Jul 2018, 15:16

Re: Regex - help simplify function, define problems

Post by DanRim » 13 Oct 2020, 17:05

@mikeyww I saw you have changed couple times the script, it was really useful to see some examples and how you think. I will borrow for sure your version because it is way more shorter than mine. :)

Question: Why loop skips single (7 digits)? Combination 1234567 FORNYELSEOCDE 1 is working, but what about single digits? 7654321

Code: Select all


^!e::
str = 
(								
Polisenr Modul Selskap
1234567 06 07
08 9911118 00 HAR FORNYELSECODE 3
Polisenr Modul Selskap
2345678

09 7654321 00 HAR FORNYELSECODE 7
04 1122334 00 HAR FORNYELSECODE 8
0125878
)

Loop, Parse, str, `n, `r
{
 ;RegExMatch(A_LoopField, "(\d\d )?(\d{7})( \d\d HAR)?( FORNYELSECODE \d)?", m) ; old version
 RegExMatch(A_LoopField, "(\d\d )?( \d{7})( \d\d HAR)?( FORNYELSECODE)?( \d)?", m) ; added m5 for single digit

; how to get single 7 digits which is in string also? 

 If StrLen(m2 m4 m5)
	;~ MsgBox, % m2 "`n" m4 "`n" m5 
	MsgBox, 64, Match on line #%A_Index%, %m2%%m4%%m5% 
}
return

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Regex - help simplify function, define problems

Post by mikeyww » 13 Oct 2020, 17:33

You tried my script above? How many matches were found? My run shows six matches. Is that accurate?

DanRim
Posts: 153
Joined: 20 Jul 2018, 15:16

Re: Regex - help simplify function, define problems

Post by DanRim » 14 Oct 2020, 14:44

@mikeyww My bad, I oversaw that variation and used old one which I saw before correction.
This script is really great and it seems easy to use!

There is one thing which I could not solve in your script:
then we run code and code meets this part in the string - 1234567 06 07 it gives back 1234567 0. How to avoid that zero? I tired to add spaces or new double digits, or use | (OR) but it did not helped.

Code: Select all


^!e::
str = 
(								
Polisenr Modul Selskap
1234567 06 07
08 9911118 00 HAR FORNYELSECODE 3
Polisenr Modul Selskap
2345678

09 7654321 00 HAR FORNYELSECODE 7
04 1122334 00 HAR FORNYELSECODE 8
0125878
)

Loop, Parse, str, `n, `r
{
 RegExMatch(A_LoopField, "(\d\d )?(\d{7})( \d\d HAR)?( FORNYELSECODE)?( \d)?", m) ;i changed to separate digit \d to get m5

 If StrLen(match := m2 m4 m5)
	MsgBox, % m2 m4 m5 ; this pars is good that I can use parts separate 
  ;~ MsgBox, %match%
}


return

Attachments
111.JPG
111.JPG (59.26 KiB) Viewed 225 times

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Regex - help simplify function, define problems

Post by mikeyww » 14 Oct 2020, 15:13

It's because you're running your script, which is not my script. You changed the code. Paste the code from my own post.

DanRim
Posts: 153
Joined: 20 Jul 2018, 15:16

Re: Regex - help simplify function, define problems

Post by DanRim » 14 Oct 2020, 15:48

@mikeyww I changed a little bit your code because I am not sure how can I reach that single digit which goes after Fornyelsecode 8.
In code it is like one combination "Fornyelsecode 8" ( FORNYELSECODE \d)?, I need have them separate some how that is why I change to ( FORNYELSECODE)?( \d), but I get for some reason additional zero which can be any number:

Some guy helped me in regex forum with some correction:
@mikeyww without your help I would not be able to learn so much! Really, thank you very much to you and your support and time!

Code: Select all


RegExMatch(A_LoopField, "(\d\d )?(\d{7})( \d\d HAR)?( FORNYELSECODE)?( \d(?!\d))?", m)

]

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Regex - help simplify function, define problems  Topic is solved

Post by mikeyww » 14 Oct 2020, 15:57

No problem. The adjustment is below. I just put the last digit into a separate variable (RegEx section bound by parentheses), which is now m5. You were close: this is almost just like the change that you made, but I put the space into the preceding section. What happened with your code is that the space followed by a digit actually matched that pair of characters towards the end of the string, which is not what you wanted. Putting the space with the previous block inside the parentheses there ensures that if FORNYELSECODE is matched, the trailing space goes with it.

Code: Select all

str =
(
Polisenr Modul Selskap
1234567 06 07
08 9911118 00 HAR FORNYELSECODE 3
Polisenr Modul Selskap
2345678

09 7654321 00 HAR FORNYELSECODE 7
04 1122334 00 HAR FORNYELSECODE 8
0125878
)

Loop, Parse, str, `n, `r
{
 RegExMatch(A_LoopField, "(\d\d )?(\d{7})( \d\d HAR)?( FORNYELSECODE )?(\d?)", m)
 If StrLen(match := m2 m4 m5)
  MsgBox, 64, Match on line #%A_Index%, %match%
}
Return

Post Reply

Return to “Ask for Help (v1)”