Regex to only copy sequential capitalized words Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Shanghei
Posts: 28
Joined: 04 Jan 2021, 22:42

Regex to only copy sequential capitalized words

16 Jun 2021, 11:10

Hello, seeking a regex to only copy sequential capitalized words in a sentence.

For example:

Good MORNING everyone. Have a good day.

So if I highlight the text above and copy, only MORNING is copied?

Thanks a bunch.
Rohwedder
Posts: 7550
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Regex to only copy sequential capitalized words  Topic is solved

16 Jun 2021, 11:25

Hallo,
try:

Code: Select all

ClipBoard = Good MORNING everyone. Have a good day.
RegExMatch(ClipBoard, "([A-Z]{2,})", M)
MsgBox,% M1
Shanghei
Posts: 28
Joined: 04 Jan 2021, 22:42

Re: Regex to only copy sequential capitalized words

16 Jun 2021, 11:32

morning was just an example, i meant cap words in general
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Regex to only copy sequential capitalized words

16 Jun 2021, 11:35

What makes you think it works only with the word "morning"? If it's because of the first line, that's just for providing the demonstration. You said you wanted the RegEx itself. Do you need the code to implement the RegEx when the clipboard gets new text copied to it?
Rohwedder
Posts: 7550
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Regex to only copy sequential capitalized words

16 Jun 2021, 11:41

Are you swedish?
Then:

Code: Select all

ClipBoard = Ha ett trevligt ÅR
RegExMatch(ClipBoard, "(\p{Lu}{2,})", M)
MsgBox,% M1
Shanghei
Posts: 28
Joined: 04 Jan 2021, 22:42

Re: Regex to only copy sequential capitalized words

16 Jun 2021, 12:18

I see sorry I added it wrong to my ahk but got it to work now as you had it is correct
Shanghei
Posts: 28
Joined: 04 Jan 2021, 22:42

Re: Regex to only copy sequential capitalized words

16 Jun 2021, 16:22

Is there any modification I can make to that to only copy words that are 3 to 4 letters only?
sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Regex to only copy sequential capitalized words

16 Jun 2021, 16:49

Shanghei wrote:
16 Jun 2021, 16:22
Is there any modification I can make to that to only copy words that are 3 to 4 letters only?
Yes, try

Code: Select all

ClipBoard = HA ETT TREVLIGT ÅR
RegExMatch(ClipBoard, "(\p{Lu}{3,4})", M)
MsgBox,% M  ; On this pattern, M and M1 give the same output

/* Output:

ETT
 */
Edited: Posted a better example
Shanghei
Posts: 28
Joined: 04 Jan 2021, 22:42

Re: Regex to only copy sequential capitalized words

16 Jun 2021, 17:05

sofista wrote:
16 Jun 2021, 16:49
Shanghei wrote:
16 Jun 2021, 16:22
Is there any modification I can make to that to only copy words that are 3 to 4 letters only?
Yes, try

Code: Select all

ClipBoard = HA ETT TREVLIGT ÅR
RegExMatch(ClipBoard, "(\p{Lu}{3,4})", M)
MsgBox,% M  ; On this pattern, M and M1 give the same output

/* Output:

ETT
 */
Edited: Posted a better example
Thanks for the quick reply, i tried the above, but if there is a word with more thatn 3 letters first, it copies that but the first four letters of that word. I want it to skip those all together. So with the script

ALERT BUY the outcome is ALER, whereas I just want BUY to be copied.
sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Regex to only copy sequential capitalized words

16 Jun 2021, 18:03

Shanghei wrote:
16 Jun 2021, 17:05
Thanks for the quick reply, i tried the above, but if there is a word with more thatn 3 letters first, it copies that but the first four letters of that word. I want it to skip those all together. So with the script

ALERT BUY the outcome is ALER, whereas I just want BUY to be copied.
You're welcome. Try this other regex, will match the first word with three or four letters:

Code: Select all

ClipBoard = ALERT BUY the outcome is ALER
RegExMatch(ClipBoard, "\b(\p{Lu}{3,4})\b", M)
MsgBox, % M

/* Output:

BUY
 */
Shanghei
Posts: 28
Joined: 04 Jan 2021, 22:42

Re: Regex to only copy sequential capitalized words

16 Jun 2021, 19:03

sofista wrote:
16 Jun 2021, 18:03
Shanghei wrote:
16 Jun 2021, 17:05
Thanks for the quick reply, i tried the above, but if there is a word with more thatn 3 letters first, it copies that but the first four letters of that word. I want it to skip those all together. So with the script

ALERT BUY the outcome is ALER, whereas I just want BUY to be copied.
You're welcome. Try this other regex, will match the first word with three or four letters:

Code: Select all

ClipBoard = ALERT BUY the outcome is ALER
RegExMatch(ClipBoard, "\b(\p{Lu}{3,4})\b", M)
MsgBox, % M

/* Output:

BUY
 */
that did it thanks
Rohwedder
Posts: 7550
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Regex to only copy sequential capitalized words

17 Jun 2021, 01:22

Just a note: For international texts \b requires (*UCP).
Try:

Code: Select all

German = ​Umlaute machen häufig ÄRGER!
RegExMatch(German, "\b\p{Lu}{2,}\b", Ascii)
RegExMatch(German, "(*UCP)\b\p{Lu}{2,}\b", Unicode)
MsgBox,% "Ascii:`t" Ascii ; RGER
MsgBox,% "Unicode:`t" Unicode ; ÄRGER
sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Regex to only copy sequential capitalized words

17 Jun 2021, 08:33

@Rohwedder Forgot to mention that. Thanks for the heads up :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CastleChou, jaka1 and 125 guests