I have the multiple rows of strings that look like the following:
irm-eap-edp-refined-nonprod
irm-eap-edp-reporting-prod
irm-eap-edp-development-nonprod
I need to extract the nonprod or prod string from each, it will always be after the 4th hyphen and the last substring of the entire string.
What's a simple regex for this situation?
If you want the last substring after - you can do:
.*-(.*)
Regex demo.
You could substitute everything starting with 4 [^-]+- chunks with nothing:
/^(?:[^-]+-){4}//gm
^ - start of line anchor
[^-]+- - match on anything but - 1 or more times followed by a -
(?: ... ) - non capturing group
{4} - four times
Demo
It looks like what you want is prod or nonprod after a hyphen, and at the end of a string, so:
-(prod|nonprod)$
This has the benefit of the prod/nonprod being in the capture group.
Related
I was able to achieve some of the output but not the right one. I am using replace all regex and below is the sample code.
final String label = "abcs-xyzed-abc-nyd-request-xyxpt--1-cnaq9";
System.out.println(label.replaceAll(
"([^-]+)-([^-]+)-(.+)-([^-]+)-([^-]+)", "$3"));
i want this output:
abc-nyd-request-xyxpt
but getting:
abc-nyd-request-xyxpt-
here is the code https://ideone.com/UKnepg
You may use this .replaceFirst solution:
String label = "abcs-xyzed-abc-nyd-request-xyxpt--1-cnaq9";
label.replaceFirst("(?:[^-]*-){2}(.+?)(?:--1)?-[^-]+$", "$1");
//=> "abc-nyd-request-xyxpt"
RegEx Demo
RegEx Details:
(?:[^-]+-){2}: Match 2 repetitions of non-hyphenated string followed by a hyphen
(.+?): Match 1+ of any characters and capture in group #1
(?:--1)?: Match optional --1
-: Match a -
[^-]+: Match a non-hyphenated string
$: End
The following works for your example case
([^-]+)-([^-]+)-(.+[^-])-+([^-]+)-([^-]+)
https://regex101.com/r/VNtryN/1
We don't want to capture any trailing - while allowing the trailing dashes to have more than a single one which makes it match the double --.
With your shown samples and attempts, please try following regex. This is going to create 1 capturing group which can be used in replacement. Do replacement like: $1in your function.
^(?:.*?-){2}([^-]*(?:-[^-]*){3})--.*
Here is the Online demo for above regex.
Explanation: Adding detailed explanation for above regex.
^(?:.*?-){2} ##Matching from starting of value in a non-capturing group where using lazy match to match very near occurrence of - and matching 2 occurrences of it.
([^-]*(?:-[^-]*){3}) ##Creating 1st and only capturing group and matching everything before - followed by - followed by everything just before - and this combination 3 times to get required output.
--.* ##Matching -- to all values till last.
I wish to match a filename with column and line info, eg.
\path1\path2\a_file.ts:17:9
//what i want to achieve:
match[1]: a_file.ts
match[2]: 17
match[3]: 9
This string can have garbage before and after the pattern, like
(at somewhere: \path1\path2\a_file.ts:17:9 something)
What I have now is this regex, which manages to match column and line, but I got stuck on filename capturing part.. I guess negative lookahead is the way to go, but it seems to match all previous groups and garbage text in the end of string.
(?!.*[\/\\]):(\d+):(\d+)\D*$
Here's a link to current implementation regex101
You can replace the lookahead with a negated character class:
([^\/\\]+):(\d+):(\d+)\D*$
See the regex demo. Details:
([^\/\\]+) - Group 1: one or more chars other than / and \
: - a colon
(\d+) - Group 2: one or more digits
: - a colon
(\d+) - Group 3: one or more digits
\D*$ - zero or more non-digit chars till end of string.
To match these examples:
1-10-1
1-7-3
10-8-5
1-7-14
11-10-12
This regex works:
^[\\d]{1,2}-[\\d]{1,2}-[\\d]{1,2}$
How could this be written in a way that just matches something like "[\d]{1,2}-?" three (n) times?
You may use:
^\d\d?(?:-\d\d?){2}$
See an online demo.
^ - Start line anchor.
\d\d? - A single digit followed by an optional one (the same as \d{1,2}).
(?:-\d\d?){2} - A non-capture group starting with an hyphen followed by the same construct as above, one or two digits. The capture group is repeated exactly two times.
$ - End string anchor.
The idea here is to avoid an optional hyphen in the attempts you made since essentially you'd start to allow whole different things like "123" and "123456". It's more appropriate to match the first element of a delimited string and then use the non-capture group to match a delimiter and the rest of the required elements exactly n-1 times.
I tried using (?P<Time>.+)\,\s(?P<Station>.+), but it did not capture the first line.
The sample strings aree:
9:21:13 AM
9:21:29 AM, TS729
9:21:33 AM, TS729
Tested at regex101.com:
You can use
^(?P<Time>[^,]+)(?:,\s*(?P<Station>.+))?$
See the regex demo (switch to Unit Tests, the link is in the left pane).
Details:
^ - start of string
(?P<Time>[^,]+) - Time group: any one or more chars other than a comma
(?:,\s*(?P<Station>.+))? - an optional sequence of
, - a comma
\s* - zero or more whitespaces
(?P<Station>.+) - one or more chars other than line break chars captured into Group "Station"
$ - end of string.
Unit tests screenshot:
Am trying to parse strings similar to these variations:
"AB-19-027654-A-1"
"AB-19-027654-A-1-2"
"ABC-19-027654-A-1"
"ABC-19-027654-A-1-2"
Looking for a way to use regular expression to have the above strings split at the third hyphen into two strings.
"AB-19-027654-A-1" split into "AB-19-027654" and "A-1"
"AB-19-027654-A-1-2" split into "AB-19-027654" and "A-1-2"
"ABC-19-027654-A-1" split into "ABC-19-027654" and "A-1"
"ABC-19-027654-A-1-2" split into "ABC-19-027654" and "A-1-2"
Have tried something like this ^(?'STRING1'.+[\d-}])-(?'STRING2'.*)-??$
but it does work for all the combinations listed.
The only consistency I can find in the original strings is that there is always at least three hyphens and the two strings I need are before and after that third hyphen accordingly.
Any ideas would be appreciated.
You can use this regex with two capture groups:
/^((?:[^-]+-?){3})-(.*)$/
Explanation:
^ - start of string
( - start capture group 1
(?:[^-]+-?){3} - non-capturing group of characters other than - followed by optional -, repeated 3 times
) - end capture group 1
- - literal -
(.*) - capture group 2: everything to end of string
$ - end of string