Extracting upper cases ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fivefive
Posts: 60
Joined: 07 May 2022, 14:16

Extracting upper cases ?

25 Apr 2023, 09:44

Hey guys,

Hope all is well. I have a favour to ask! Basically, I got a list product code in letters and they go like this: -

Purchased A B C D on First Monday in June
Purchased B B B on First Tuesday in July
Purchased C C C C on Second Monday in August
Purchased D D D D on Third Monday in September
Purchased E E E on Fourth Monday in September

I would like to extract if possible: -

ABCD
BBB
CCCC
DDDD
EEE

Please note: -

The product codes could be 3 or 4 letters long.

Any idea please? I would be really grateful!

Many thanks
User avatar
mikeyww
Posts: 27270
Joined: 09 Sep 2014, 18:38

Re: Extracting upper cases ?

25 Apr 2023, 10:19

Code: Select all

#Requires AutoHotkey v1.1.33

str := "
(
Purchased A B C D on First Monday in June
Purchased B B B on First Tuesday in July
Purchased C C C C on Second Monday in August
Purchased D D D D on Third Monday in September
I Purchased E E E on Fourth Monday in September
I Purchased E       E E on Fourth Monday in September
I Purchased EEE on Fourth Monday in September
I Purchased EE E on Fourth Monday in September
Purchased F F on Fifth Monday in September
Purchased g g g on Sixth Monday in September
)"
MsgBox % prodCodes(str)

prodCodes(str) {
 Static regex := "([A-Z] *){3,4}"
 Loop Parse, % str, `n, `r
  RegExMatch(A_LoopField, regex, m), out .= RegExReplace(m, "\h+") "`n"
 Return Trim(out, "`n")
}
fivefive
Posts: 60
Joined: 07 May 2022, 14:16

Re: Extracting upper cases ?

25 Apr 2023, 10:42

mikeyww wrote:
25 Apr 2023, 10:19

Code: Select all

#Requires AutoHotkey v1.1.33

str := "
(
Purchased A B C D on First Monday in June
Purchased B B B on First Tuesday in July
Purchased C C C C on Second Monday in August
Purchased D D D D on Third Monday in September
I Purchased E E E on Fourth Monday in September
I Purchased E       E E on Fourth Monday in September
I Purchased EEE on Fourth Monday in September
I Purchased EE E on Fourth Monday in September
Purchased F F on Fifth Monday in September
Purchased g g g on Sixth Monday in September
)"
MsgBox % prodCodes(str)

prodCodes(str) {
 Static regex := "([A-Z] *){3,4}"
 Loop Parse, % str, `n, `r
  RegExMatch(A_LoopField, regex, m), out .= RegExReplace(m, "\h+") "`n"
 Return Trim(out, "`n")
}
Thanks a lot for your help! Actually, just thought another issue: -

I Purchased EEE On Fourth Monday in September

In this case, it would return "EEEO", any way to get rid of the O please? (If On has the O in uppercase)

I would be really grateful! Many thanks
User avatar
mikeyww
Posts: 27270
Joined: 09 Sep 2014, 18:38

Re: Extracting upper cases ?

25 Apr 2023, 11:06

We have reached the "That works, but what if...?" part of the tutorial!

Show all of your relevant examples, and describe a rule that you would like to use to distinguish matches from non-matches. We can then pursue a solution once instead of many times.
fivefive
Posts: 60
Joined: 07 May 2022, 14:16

Re: Extracting upper cases ?

25 Apr 2023, 11:18

mikeyww wrote:
25 Apr 2023, 11:06
We have reached the "That works, but what if...?" part of the tutorial!

Show all of your relevant examples, and describe a rule that you would like to use to distinguish matches from non-matches. We can then pursue a solution once instead of many times.
Thanks very much!! Sorry about this. This would be the last criteria: -

So if they contain E E E This

Then it currently generates EEET instead of EEE that is needed.

I would be really grateful! Many thanks!
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Extracting upper cases ?

25 Apr 2023, 11:24

just add \b

Code: Select all

prodCodes(str) {
 Static regex := "([A-Z] *){3,4}\b"
 Loop Parse, % str, `n, `r
  RegExMatch(A_LoopField, regex, m), out .= RegExReplace(m, "\h+") "`n"
 Return Trim(out, "`n")
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Nick Riviera and 87 guests