regex conditional statement (only parse numbers if string has a certain beginning) -
first used string returned relaystates, “1.0.0.0.1.1.0.0” parsed/grouped \d+, 8 switches used ‘format response’, e.g. {1} state each switch.
now need numbers out of string: “relays.1.0.0.0.1.1.0.0”
\d+ still numbers want them if string starts “relays"
can please explain how that?
thnx million in advance! edited icebear (today 00:24)
with .net engine, use regex (?<=^relays[\d.]*)\d+. regex engines don't support indefinite repetition in negative lookbehind assertion.
explanation:
(?<= # assert following can matched before current position: ^relays # start of string, followed "relays" [\d.]* # , number of digits/dots. ) # end of lookbehind assertion \d+ # match 1 or more digits. with pcre engine, use (?:^relays\.|\g\.)(\d+) , access group 1 each match.
see live on regex101.com.
explanation:
(?: # start non-capturing group matches... ^relays\. # either start of string , "relays." | # or \g\. # position after previous match, followed "." ) # end of non-capturing group (\d+) # match number , capture in group 1
Comments
Post a Comment