Page 1 of 1

Help with RegEx

Posted: Tue Feb 24, 2015 4:24 pm
by Terkelsen
I have a string that looks like this:

FAKEAOS-nofrt-nofrt-000411-004

...and I need a RegEx to retrieve everything after the second hyphen (nofrt-000411-004). Can anybody help me with that?

Re: Help with RegEx

Posted: Wed Feb 25, 2015 11:12 am
by bens
Hi,

As always with regular expressions there are several possibilities. Here is one:

Code: Select all

	var theString = "FAKEAOS-nofrt-nofrt-000411-004";
	var theRegex = /[A-Za-z0-9]+-[A-Za-z0-9]+-([A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+)/;
	theRegex.search( theString );
	var theResult = theRegex.cap(1);
The line "theRegex.cap(1)" will return the second capture (remember this is 0-based!), which is the part of the regular expression between parentheses.

Re: Help with RegEx

Posted: Wed Feb 25, 2015 11:54 am
by loicaigon
Hi,

As bens said, many solutions. Might this one be "lighter" :

Code: Select all

var theRegex = /.+-.+-(.+)/;
Loic
http://www.ozalto.com

Re: Help with RegEx

Posted: Thu Feb 26, 2015 8:39 am
by Terkelsen
Thank you both for the help. It seems that bens suggestion will return what I wanted, whereas loicaigon's will only return everything after the last hyphen.

I have tested both og regex101.com. I'm not using this in a JavaScript but directly in a variable in Switch.

Re: Help with RegEx

Posted: Thu Feb 26, 2015 12:06 pm
by tz8
loicaigon wrote:Hi,

As bens said, many solutions. Might this one be "lighter" :

Code: Select all

var theRegex = /.+-.+-(.+)/;
let me fix that one:

Code: Select all

var theRegex = /^[^-]+-[^-]+-(.+$)/;

Re: Help with RegEx

Posted: Thu Feb 26, 2015 3:19 pm
by Terkelsen
Thank you tz8. That seems to do the trick as well but with a much more simple expression.

Re: Help with RegEx

Posted: Thu Feb 26, 2015 4:17 pm
by tz8
i'm hinting everyone to this fantastic site then that i normally use to find out what doing with regexes: regexr.com

Re: Help with RegEx

Posted: Mon Mar 02, 2015 1:31 pm
by Terkelsen
Wel, well, well. It all seems to work in any RegEx-tester but not inside Switch. It turns out that ^(Cone) has a different functionality in Switch than in other applications when used outside the square brackets.

That means that this:
^[^-]+-[^-]+-
and this
[^-]+-[^-]+-
will both return the same result, namely everything up till and including the second hyphen. ^ was supposed to exclude the following but in Switch it just marks the beginning of the string. In other words this:
^[^-]+-[^-]+-(.+$)
and this
[^-]+-[^-]+-(.+$)
will both just return the entire string.

Does anybody have an idea how to exclude the first part in a way that will work in Switch?

Re: Help with RegEx

Posted: Mon Mar 02, 2015 4:55 pm
by BuckXUK
Hi

This works for me, using a fixed length positive look-behind:

(?<=[A-Z]{7}-[a-z]{5}-).*$

Re: Help with RegEx

Posted: Mon Mar 09, 2015 2:22 pm
by BuckXUK
Actually, this is what you need to use in Switch:

[^A-Z-]+[^a-z-]?[a-z-](?=[0-9]{6}).*

Positive look behinds are a no-go as well as negative ones.

I hope this helps.

Re: Help with RegEx

Posted: Tue Mar 10, 2015 2:55 pm
by bens
Terkelsen wrote:Wel, well, well. It all seems to work in any RegEx-tester but not inside Switch.
That's a problem with regular expressions in general. Many applications all have their own subset and/or extensions of regular expressions. Even such basic commands as grep, sed and awk don't agree completely on their regular expressions. Websites to build and test regular expressions can help, but if you're doing anything exotic(*) you'll only be sure once you test it in the actual target application.

(*) Some might argue that using regular expressions in and of itself is already exotic. Hence the (in)famous quote "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.".