问题
I need to determine whether a string begins with a number - I've tried the following to no avail:
if (matches("^[0-9].*)", upper(text))) str = "Title"""
I'm new to DXL and Regex - what am I doing wrong?
回答1:
You need the caret character to indicate a match only at the start of a string. I added the plus character to match all the numbers, although you might not need it for your situation. If you're only looking for numbers at the start, and don't care if there is anything following, you don't need anymore.
string str1 = "123abc"
string str2 = "abc123"
string strgx = "^[0-9]+"
Regexp rgx = regexp2(strgx)
if(rgx(str1)) { print str1[match 0] "\n" } else { print "no match\n" }
if(rgx(str2)) { print str2[match 0] "\n" } else { print "no match\n" }
The code block above will print:
123
no match
回答2:
@mrhobo is correct, you want something like this:
Regexp numReg = "^[0-9]"
if(numReg text) str = "Title"
You don't need upper
since you are just looking for numbers. Also matches
is more for finding the part of the string that matches the expression. If you just want to check that the string as a whole matches the expression then the code above would be more efficient.
Good luck!
回答3:
At least from example I found this example should work:
Regexp plural = regexp "^([0-9].*)$" if plural "15systems" then print "yes"
Resource: http://www.scenarioplus.org.uk/papers/dxl_regexp/dxl_regexp.htm
来源:https://stackoverflow.com/questions/19135415/in-doors-dxl-how-do-i-use-a-regular-expression-to-determine-whether-a-string-st