Forum Discussion
zachedwards
Microsoft
Oct 11, 2019Regex assert position at beginning of string
I am trying to get the 2 matches found at the beginning of the string. According to this page, you can use \A to match beginning of the line. The re2 engine syntax also permits ^ to assert position to beginning of string.
In this case I want the first 2 <Param>value</Param>. The expected return value is ["a","b"] but instead the return is [["a","b"],["c","d"]]
print extract_all("^<Param>([^<]*)</Param><Param>([^<]*)</Param>", "<Param>a</Param><Param>b</Param><Param>c</Param><Param>d</Param><Param>e</Param>")
print extract_all("\\A<Param>([^<]*)</Param><Param>([^<]*)</Param>", "<Param>a</Param><Param>b</Param><Param>c</Param><Param>d</Param><Param>e</Param>")
I know you could use the parse operator, but in this case I need to specify a regex.
- CliveWatson
Microsoft
How about?
print Id="<Param>a</Param><Param>b</Param><Param>c</Param><Param>d</Param><Param>e</Param>" | extend rawExtract = extract_all(@"^<Param>([^<]*)</Param><Param>([^<]*)</Param>", dynamic([1,2]), Id) | extend returnFirst = rawExtract[0]
- zachedwards
Microsoft
Yes, although that works in this scenario, it doesn't answer why the ^ and \A are not honored in these regex patterns 😞