Help with RegEx

Post Reply
User avatar
Terkelsen
Member
Posts: 126
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Help with RegEx

Post 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?
bens
Member
Posts: 130
Joined: Thu Mar 03, 2011 10:13 am

Re: Help with RegEx

Post 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.
loicaigon
Member
Posts: 147
Joined: Wed Jul 10, 2013 10:22 am

Re: Help with RegEx

Post by loicaigon »

Hi,

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

Code: Select all

var theRegex = /.+-.+-(.+)/;
Loic
http://www.ozalto.com
User avatar
Terkelsen
Member
Posts: 126
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: Help with RegEx

Post 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.
tz8
Member
Posts: 84
Joined: Mon Aug 13, 2012 12:56 pm

Re: Help with RegEx

Post 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 = /^[^-]+-[^-]+-(.+$)/;
User avatar
Terkelsen
Member
Posts: 126
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: Help with RegEx

Post by Terkelsen »

Thank you tz8. That seems to do the trick as well but with a much more simple expression.
tz8
Member
Posts: 84
Joined: Mon Aug 13, 2012 12:56 pm

Re: Help with RegEx

Post by tz8 »

i'm hinting everyone to this fantastic site then that i normally use to find out what doing with regexes: regexr.com
User avatar
Terkelsen
Member
Posts: 126
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: Help with RegEx

Post 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?
BuckXUK
Newbie
Posts: 16
Joined: Thu Mar 01, 2012 8:43 pm

Re: Help with RegEx

Post by BuckXUK »

Hi

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

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

William Buckingham
BuckXUK
Newbie
Posts: 16
Joined: Thu Mar 01, 2012 8:43 pm

Re: Help with RegEx

Post 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.
Regards,

William Buckingham
bens
Member
Posts: 130
Joined: Thu Mar 03, 2011 10:13 am

Re: Help with RegEx

Post 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.".
Post Reply