Is it possible to have a .net regex pattern that references the numeric value of a previously captured group in another part of the pattern? I need to match strings that repeat the following format:
- 10-byte alpha key followed by 2-byte operator (subset of sql operators) followed by 5 integer digits followed by an n-byte value, where n = the integer value of the 5 integer digits.
EX String:
“key1 = 00004val1key2 <=00006value2key3 >=00011value_three”
Where val1 is 4-bytes, value2 is 6-bytes, value_three is 11-bytes, etc…. This string represents repeating set of criteria that I need to be able to parse and identify the following strings:
1st Criteria:
“key1”, “= “, “val1”
2nd Criteria:
“key2”, “<=”, “value2”
3rd Criteria:
“key3”, “>=”, “value_three”
I’ve used .net regex’s in the past and should be able to figure out parsing the key, operator and digits in a repeating pattern, but don’t know how, or even if, it is possible to reference the numeric value of the digits (i.e. an integer with value of 4 for the string “00004”) later in the pattern.
Update 1
Originally I was thinking that I needed a regex pattern such as:
^(?<criteria>(?<key>\w{10})(?<operator>(= |<=|>=))(?<value_length>\d{5})(?<value>\w{n}))+$
where the {n} quantifier in the “value” capturing group needs to have a value based on the value of the “value_length” capturing group, for each “criteria” captured. If this is not the right way to think about forming the regex pattern, I can change to another approach that works.
You can instead capture the required length string in the match itself till the next
\w{10}and operator(= |<=|>=)using lookahead