RegEx Lookahead Help

62 views
Skip to first unread message

Cindy

unread,
Jun 7, 2014, 6:58:24 PM6/7/14
to tas...@googlegroups.com
Given the below XML, how do I extract a name based on a given phone number? For example, if the phone number matches '2222222222' then how would I extract Cindy (highlighted in 'yellow' below)?

<REC><NM>Mike</NM><PHONENB>1111111111</PHONENB><PARMS></PARMS><LOCNM>TOL</LOCNM><LOCNM>Home</LOCNM></REC><REC><NM>Cindy</NM><PHONENB>2222222222</PHONENB><PARMS></PARMS><LOCNM>Wash DC</LOCNM><LOCNM>Home</LOCNM></REC>

I believe this requires a lookahead for the phone number like the following...

(<NM>(.+?)</NM>)(?=<PHONENB>2222222222</PHONENB>)

However, the above extracts everything from the first name up to the phone number match, argggg!!!
<NM>Mike</NM><PHONENB>1111111111</PHONENB><PARMS></PARMS><LOCNM>TOL</LOCNM><LOCNM>Home</LOCNM></REC><REC><NM>Cindy</NM>

Please help,
Cindy

Bob Hansen

unread,
Jun 7, 2014, 7:33:06 PM6/7/14
to tas...@googlegroups.com
One way is to take the XML and do a Variable Search Replace to replace </REC> with a carriage return. Then do the regex search on the resulting XML with carriage returns. By breaking the records into separate lines it avoids the problem you encountered.

Searching for:  (?<=<NM>).+(?=</NM><PHONENB>2222222222</PHONENB>)
will return "Cindy".
Of course, the regex could be further simplified, but this works.

Cindy

unread,
Jun 7, 2014, 8:09:25 PM6/7/14
to tas...@googlegroups.com
Thanks Bob, 

I tried it in RegexBuddy and it worked! 

> One way is to take the XML and do a Variable Search Replace to replace </REC> with a carriage return.

Question, what syntax (i.e. '\r') creates a carriage return using Tasker's Variable Search Replace?

Cindy

Bob Hansen

unread,
Jun 7, 2014, 8:13:49 PM6/7/14
to tas...@googlegroups.com
Use a Variable Set to set %return to a <enter key> and then for the replace user %return.

Cindy

unread,
Jun 7, 2014, 8:16:38 PM6/7/14
to tas...@googlegroups.com
Gotcha, 
Thanks!
Cindy

Cindy

unread,
Jun 8, 2014, 10:51:51 AM6/8/14
to tas...@googlegroups.com
I got it working as you described in tasker. However, now I'm trying to create hybrid Tasker/Javascript version and I can't figure out how to create a carriage return in JavaScript that Tasker will accept.  Below is my barebones test code to insert a carriage return using javascript and then read that carriage return in tasker...

A1: JavaScriptlet [ Code:setGlobal('Foo', 'one\r\ntwo\r\nthree\r\n'); Libraries: Auto Exit:On Timeout (Seconds):45 ]
A2: Variable Split [ Name:%Foo Splitter:%CarriageReturn Delete Base:Off ]
A3: Popup [ Title: Text: Foo2 - %Foo2
Foo(:) - %Foo(:) Background Image: Layout:Popup Timeout (Seconds):600 Show Over Keyguard:On ]

Note, I'm using the base variable for global regex queries. The array lets me iterate to the exact item.
Note2, variable %CarriageReturn is set to an <enter key>.

The output from the above script is: 

Foo2 - %Foo2 Foo(:) - one 
two
three

I'm expecting: Foo2 - two Foo(:) - onetwothree

Thank you for your help!

Cindy

Cindy

unread,
Jun 8, 2014, 11:25:50 AM6/8/14
to tas...@googlegroups.com
Got it working! Sorry, needed to make sure %CarriageReturn was set to <enter key>.  Somehow it was cleared out previous to running my test.

Thanks again.
Cindy
Reply all
Reply to author
Forward
0 new messages