I cannot think of a regex for this task at the moment, albeit it may be possible with some clever lookarounds or the like
. I can only propose the following:
1) match "numbers and/or letters"
2) check the captured part of matches for "is not only letters"
; match one or more letters/digits enclosed in "-", capture the letters/digits
if ($regex(SOMETEXT,/-([a-z0-9]+)-/ig)) {
; loop captured matches
var %n = 1
while ($regml(%n)) {
; capture isn't letters only
if ($v1 !isalpha) {
ECHO -a do something with capture $v1
break
}
inc %n
}
}
If you apply this code on: " Test-ing-it -a5B-Foobar --XX7C--5-! " for example, it will process "a5B" (because "ing" is skipped for being only letters) and stop. If you want to process the subsequent matches "XX7C" and "5" as well, remove the "break" command in the while loop.
_________
EDIT
Note that the regex above "consumes" alle the "-"chars while matching. Using the snippet above, a check on
will
only process "a5":
- "-ing-" is matched first,
including it's leading and trailing "-"
- "-ing-" is skipped in processing, for being letters only
- the check now continues at "5- ..." (
not at "
-5- ...")
- thus "-a5-" is matched and will be processed
- the regex now continues at "1z- ...", therefore
- the next match is -zz-, which is skipped ... etc
You can use lookarounds to prevent the "consumption" of the "-"chars in the matching:
if ($regex(SOMETEXT,/
(?<=-)([a-z0-9]+)
(?=-)/ig)) {
... will process the second match "5" (the first match that passes the !isalpha condition). And if you didn't use /break, the "a5" "1z" and again "1z" as well.
You'll find a better explanation of lookarounds
here