Using RegExMatch with a "range" of expressions Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
JstPlyr14
Posts: 3
Joined: 25 Jul 2019, 22:36

Using RegExMatch with a "range" of expressions

25 Jul 2019, 22:54

Hi, I'm very new to coding but I'm trying to create something here.

I'm using the following command to check if a sentence is present in my clipboard (regardless of position):

Code: Select all

If RegExMatch( Clipboard, "red cars" )
and it's working but the data I have always contains a number before ("# red cars").

How can I make the "If" command only be true for when that number is equal to or higher than 5?

For example, I want the match "6 red cars" to be true but "4 red cars" to be false.

Thank you all.
aifritz
Posts: 144
Joined: 29 Jul 2018, 11:30
Location: Germany

Re: Using RegExMatch with a "range" of expressions  Topic is solved

25 Jul 2019, 23:34

Code: Select all

Clipboard =  There are 499 red cars in the garage
RegExMatch( Clipboard, "O)(\d*)\s(red cars)", hit)
If (hit.value[1] >= 5)  ;hit.value(N): Returns the overall match or a captured subpattern.
	Msgbox % hit.value[1] " " hit.value[2]
else
	Msgbox % "Sorry only " hit.value[1] " " hit.value[2]

JstPlyr14
Posts: 3
Joined: 25 Jul 2019, 22:36

Re: Using RegExMatch with a "range" of expressions

26 Jul 2019, 00:08

aifritz wrote:
25 Jul 2019, 23:34

Code: Select all

Clipboard =  There are 499 red cars in the garage
RegExMatch( Clipboard, "O)(\d*)\s(red cars)", hit)
If (hit.value[1] >= 5)  ;hit.value(N): Returns the overall match or a captured subpattern.
	Msgbox % hit.value[1] " " hit.value[2]
else
	Msgbox % "Sorry only " hit.value[1] " " hit.value[2]

Thanks! This worked for what I wanted.

Just as curiosity. What exactly does ---O)(\d*)\s--- means? I know \s is the blank character and \d are the digits. but what is the "O)" for?
If the data was "+6 red cars" for example... would this still work? Or you'd have to specify the "+" somehow?

Thank you!
User avatar
hoppfrosch
Posts: 368
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: Using RegExMatch with a "range" of expressions

26 Jul 2019, 00:16

You might use this regular expression to match only numbers greater/equal than 6:

Code: Select all

\s+([6-9]|[1-9]\d{1,})\s+
In detail:

Code: Select all

\s+ ;one or more spaces

Code: Select all

([6-9]|[1-9]\d{1,}) ;number matching
  • Code: Select all

    [6-9] ;one digit from range 6-9 ==> matches numbers 6 to 9
  • Code: Select all

    | ; OR
  • Code: Select all

    [1-9]\d{1,} ;one digit from range 1 to 9 followed by at least 1 up to N any digits  ==> matches numbers up from 10, whilst numbers with leading 0 (like 02) are not matched

Code: Select all

\s+ ;one or more spaces
see: https://regex101.com/r/PwkzsK/1/tests
Last edited by hoppfrosch on 26 Jul 2019, 00:36, edited 1 time in total.
Odlanir
Posts: 514
Joined: 20 Oct 2016, 08:20

Re: Using RegExMatch with a "range" of expressions

26 Jul 2019, 00:35

aifritz wrote:
25 Jul 2019, 23:34
Just as curiosity. What exactly does ---O)(\d*)\s--- means? I know \s is the blank character and \d are the digits. but what is the "O)" for?
https://www.autohotkey.com/docs/commands/RegExMatch.htm#MatchObject
aifritz wrote:
25 Jul 2019, 23:34
If the data was "+6 red cars" for example... would this still work? Or you'd have to specify the "+" somehow?
Yes but with the above Regex the plus sign will not be reported in the match. To get also the + or - char preceding the digit you can use:

Code: Select all

RegExMatch( clipboard, "O)([+-]?\d*)\s(red cars)", hit)
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Flipeador, Google [Bot], teadrinker, viceversa, yjs778 and 249 guests