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?
Help with RegEx
Hi,
As always with regular expressions there are several possibilities. Here is one:
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.
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);
Hi,
As bens said, many solutions. Might this one be "lighter" :
Loic
http://www.ozalto.com
As bens said, many solutions. Might this one be "lighter" :
Code: Select all
var theRegex = /.+-.+-(.+)/;http://www.ozalto.com
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.
I have tested both og regex101.com. I'm not using this in a JavaScript but directly in a variable in Switch.
let me fix that one:loicaigon wrote:Hi,
As bens said, many solutions. Might this one be "lighter" :Code: Select all
var theRegex = /.+-.+-(.+)/;
Code: Select all
var theRegex = /^[^-]+-[^-]+-(.+$)/;Thank you tz8. That seems to do the trick as well but with a much more simple expression.
i'm hinting everyone to this fantastic site then that i normally use to find out what doing with regexes: regexr.com
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?
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?
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.Terkelsen wrote:Wel, well, well. It all seems to work in any RegEx-tester but not inside Switch.
(*) 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.".