0
votes

I am using an input box to request a string from the user that has the form "sometext5". I would like to separate this via regexp into a variable for the string component and a variable for the number. The number then shall be used in a loop.

The following just returns "0", even when I enter a string in the form "itemize5"

!n::
InputBox, UserEnv, Environment, Please enter an environment!, , 240, 120
If ErrorLevel
    return
Else
    FoundPos := RegExMatch(%UserEnv%, "\d+$")
    MsgBox %FoundPos%
retur

n

1

1 Answers

1
votes
  1. FoundPos, as its name implies, contains the position of the leftmost occurrence of the needle. It does not contain anything you specifically want to match with your regex.
  2. When passing variable contents to a function, don't enclose the variable names in percent signs (like %UserEnv%).
  3. Your regex \d+$ will only match numbers at the end of the string, not the text before it.

A possible solution:

myText := "sometext55"
if( RegExMatch(myText, "(.*?)(\d+)$", splitted) ) {
    msgbox, Text: %splitted1%`nNumber: %splitted2%
}

As described in the docs, splitted will be set to a pseudo-array (splitted1, splitted2 ...), with each element containing the matched subpattern of your regex (the stuff that is in between round brackets).