Non-Greedy Pattern Matching

Post Reply
patgilmour
Newbie
Posts: 6
Joined: Mon Jul 06, 2015 10:56 pm

Non-Greedy Pattern Matching

Post by patgilmour »

I'm trying to do a non-greedy pattern match using Regex to get a 3-6 digit order number.

The standard PCRE Search Pattern is:(Order No)(.*?)(\d{3,6})
Then I get the order number using $3 (or \3 for Switch)

The problem is .*? in Switch is greedy and so it grabs everything to the end of the file/string.

So, is there a way to restrict the search after 'Order No' to "any amount of any characters up to the next 3-6 digit integer?"

Many thanks, Pat
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Non-Greedy Pattern Matching

Post by gabrielp »

Could you describe where you are doing this matching? Perhaps take a screenshot of where you are putting your current search pattern. The answer could go a few different ways depending on if this is within the variable builder in Switch, Scripter, or elsewhere.
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
patgilmour
Newbie
Posts: 6
Joined: Mon Jul 06, 2015 10:56 pm

Re: Non-Greedy Pattern Matching

Post by patgilmour »

Thanks for the reply.

I have been trying to do this in a variable builder within a flow. It examines a 'body' node from an 'Email' metadata set.

An example of the string it would try for the pattern match within:

Order No</span></b><span style="font-size: 9pt;" class=""><span class="Apple-converted-space">&nbsp;</span>033174<

My main interest is in knowing if there is standard method in Switch of doing any any character non-greedy match, as I've stumbled on this before.
loicaigon
Member
Posts: 147
Joined: Wed Jul 10, 2013 10:22 am

Re: Non-Greedy Pattern Matching

Post by loicaigon »

Hi,

What about this : \d{3,6}$

That would catch any 3-6 numbers at the end of a string. If you need to ensure that it's not a false positive, you can filter the whole match first.

HTH

Loic
www.ozalto.com
patgilmour
Newbie
Posts: 6
Joined: Mon Jul 06, 2015 10:56 pm

Re: Non-Greedy Pattern Matching

Post by patgilmour »

Thanks loicaigon - however the integer isn't at the end of a line, so it won't match.
patgilmour
Newbie
Posts: 6
Joined: Mon Jul 06, 2015 10:56 pm

Re: Non-Greedy Pattern Matching

Post by patgilmour »

We found a solution.

In the end, we went with a Script and used this pattern to match the required string:

Code: Select all

var regExEmailBody = /(Order No)(.*)(\/span\>)([\d]{3,6})(.*)(Sequential\sID)/;
The trick was to find something consistent *after* the required string, and we had "(Sequential ID)" to match against

With that we were able to get the integer after the html span tags and pick out the 4th group using:

Code: Select all

var orderID = regExEmailBody.capturedTexts[4];
Thanks for the help!
Post Reply