Regex assert position at beginning of string

%3CLINGO-SUB%20id%3D%22lingo-sub-909636%22%20slang%3D%22en-US%22%3ERegex%20assert%20position%20at%20beginning%20of%20string%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-909636%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20get%20the%202%20matches%20found%20at%20the%20beginning%20of%20the%20string.%20According%20to%20%3CA%20title%3D%22Regular%20expressions%20reference%20doc%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fkusto%2Fquery%2Fre2%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ethis%20page%3C%2FA%3E%2C%20you%20can%20use%20%5CA%20to%20match%20beginning%20of%20the%20line.%20The%20re2%20engine%20syntax%20also%20permits%20%5E%20to%20assert%20position%20to%20beginning%20of%20string.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20case%20I%20want%20the%20first%202%20%3COBJECT%3E%3CPARAM%20%2F%3Evalue.%20The%20expected%20return%20value%20is%20%5B%22a%22%2C%22b%22%5D%20but%20instead%20the%20return%20is%26nbsp%3B%3CSPAN%3E%5B%5B%22a%22%2C%22b%22%5D%2C%5B%22c%22%2C%22d%22%5D%5D%3C%2FSPAN%3E%3C%2FOBJECT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eprint%20extract_all(%22%5E%3C%2FCODE%3E%3COBJECT%3E%3CCODE%3E(%5B%5E%26lt%3B%5D*)%3CPARAM%20%2F%3E(%5B%5E%26lt%3B%5D*)%22%2C%20%22%3CPARAM%20%2F%3Ea%3CPARAM%20%2F%3Eb%3CPARAM%20%2F%3Ec%3CPARAM%20%2F%3Ed%3CPARAM%20%2F%3Ee%22)%0Aprint%20extract_all(%22%5C%5CA%3CPARAM%20%2F%3E(%5B%5E%26lt%3B%5D*)%3CPARAM%20%2F%3E(%5B%5E%26lt%3B%5D*)%22%2C%20%22%3CPARAM%20%2F%3Ea%3CPARAM%20%2F%3Eb%3CPARAM%20%2F%3Ec%3CPARAM%20%2F%3Ed%3CPARAM%20%2F%3Ee%22)%3C%2FCODE%3E%3C%2FOBJECT%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20know%20you%20could%20use%20the%20parse%20operator%2C%20but%20in%20this%20case%20I%20need%20to%20specify%20a%20regex.%26nbsp%3B%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-909636%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EQuery%20Language%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-911303%22%20slang%3D%22en-US%22%3ERe%3A%20Regex%20assert%20position%20at%20beginning%20of%20string%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-911303%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F335421%22%20target%3D%22_blank%22%3E%40zaedwa%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHow%20about%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eprint%20Id%3D%22%3C%2FCODE%3E%3COBJECT%3E%3CCODE%3Ea%3CPARAM%20%2F%3Eb%3CPARAM%20%2F%3Ec%3CPARAM%20%2F%3Ed%3CPARAM%20%2F%3Ee%22%0A%7C%20extend%20rawExtract%20%3D%20extract_all(%40%22%5E%3CPARAM%20%2F%3E(%5B%5E%26lt%3B%5D*)%3CPARAM%20%2F%3E(%5B%5E%26lt%3B%5D*)%22%2C%20dynamic(%5B1%2C2%5D)%2C%20Id)%20%0A%7C%20extend%20returnFirst%20%3D%20rawExtract%5B0%5D%3C%2FCODE%3E%3C%2FOBJECT%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-946717%22%20slang%3D%22en-US%22%3ERe%3A%20Regex%20assert%20position%20at%20beginning%20of%20string%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-946717%22%20slang%3D%22en-US%22%3EYes%2C%20although%20that%20works%20in%20this%20scenario%2C%20it%20doesn't%20answer%20why%20the%20%5E%20and%20%5CA%20are%20not%20honored%20in%20these%20regex%20patterns%20%3A(%3C%2Fimg%3E%3C%2FLINGO-BODY%3E
Microsoft

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. 

2 Replies

@zaedwa 

 

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]
Yes, although that works in this scenario, it doesn't answer why the ^ and \A are not honored in these regex patterns :(