regex to match substring after nth occurence of pipe character
To match part after nth
occurrence of pipe you can use this regex:
/^(?:[^|]*\|){3}([^|]*)/
Here n=3
It will match 10.15.194.25
in matched group #1
RegEx Demo
Retrieve characters after nth occurrence of an another with Regex
Instead of .*?
. Then you could match everything but a forward slash by doing [^\/]*
.
([^\/]*)\s*\[
Live preview
If it needs to be after the second slash. As in the contents between the second slash and the square bracket can contain slashes. Then you could do:
(?:.*?\/){2}(.*)\s*\[
Live preview
Remove the \s*
if you want to. I'm just assuming you don't care about that whitespace.
match after nth occurrence in R stringr
Are you looking for something like this? Should capture everything between pipes.
r"(?<=\|)[^\|]*(?=\|)"
pcre regex match nth occurence
You could use an anchor to assert the start of the line and then repeat not matching a |
followed by a |
2 times. Then capture the third part in a capturing group which will contain cccc ccccccc
^(?:[^|]*\|){2}([^|]*)
Regex demo
Explanation
^
Assert the start of the line(?:
Start non capturing group[^|]*\|
Match not a|
using a negated character class zero or more times followed by a|
.
){2}
close non capturing group and repeat that 2 times([^|]*)
Capture in a group matching not a|
zero or more times
How to get character or string after nth occurrence of pipeline '|' symbol in ORACLE using REGULAR_EXPRESSION?
Here ya go. Replace the 4th argument to regexp_substr() with the number of the field you want.
with tbl(str) as (
select 'Jack|Sparrow|17-09-16|DY7009|Address at some where|details ' from dual
)
select regexp_substr(str, '(.*?)(\||$)', 1, 4, NULL, 1) field_4
from tbl;
FIELD_4
--------
DY7009
SQL>
To list all the fields:
with tbl(str) as (
select 'Jack|Sparrow|17-09-16|DY7009|Address at some where|details ' from dual
)
select regexp_substr(str, '(.*?)(\||$)', 1, level, NULL, 1) split
from tbl
connect by level <= regexp_count(str, '\|')+1;
SPLIT
-------------------------
Jack
Sparrow
17-09-16
DY7009
Address at some where
details
6 rows selected.
SQL>
So if you want select fields you could use:
with tbl(str) as (
select 'Jack|Sparrow|17-09-16|DY7009|Address at some where|details ' from dual
)
select
regexp_substr(str, '(.*?)(\||$)', 1, 1, NULL, 1) first,
regexp_substr(str, '(.*?)(\||$)', 1, 2, NULL, 1) second,
regexp_substr(str, '(.*?)(\||$)', 1, 3, NULL, 1) third,
regexp_substr(str, '(.*?)(\||$)', 1, 4, NULL, 1) fourth
from tbl;
Note this regex handles NULL elements and will still return the correct value. Some of the other answers use the form '[^|]+'
for parsing the string but this fails when there is a NULL element and should be avoided. See here for proof: https://stackoverflow.com/a/31464699/2543416
RegEx: Match nth occurrence
You can use this regex:
\b_name=(?:[^"]*"){3}
RegEx Demo
RegEx Details:
\b_name
: Match full word_name
:=
: Match a=
(?:[^"]*"){3}
: Match 0 or more non-"
characters followed by a"
. Repeat this group 3 times.
Match all after nth occurrence of specified character
I would propose this:
[^"]*(?=";)
This only matches the part within quotes, excluding (but requiring) the ending ";
.
See demo on regex101
How do I return only the nth match using a regular expression?
You could use
^(?:[^|]*\|){6}([^|]*)
Demo
In your example Another random phrase
would be saved to capture group 1.
The regex engine performs the following operations.
^ # match beginning of line
(?: # begin a non-capture group
[^|]*\| # match 0+ chars other than '|' followed by '|'
) # end non-capture group
{6} # execute non-capture group 6 times
([^|]*) # match 0+ chars other than '|' in capture group 1
If your regex engine supports \K
(as does PCRE (PHP), P and others), you don't need a capture group:
^(?:[^|]*\|){6}\K[^|]*
Demo
In your example this matches Another random phrase
.
^(?:[^|]*\|){6}\K[^|]*
The regex engine performs the following operations.
^ # match beginning of line
(?: # begin a non-capture group
[^|]*\| # match 0+ chars other than '|' followed by '|'
) # end non-capture group
{6} # execute non-capture group 6 times
\K # discard everything matched so far
[^|]* # match 0+ chars other than '|'
Regex - Match everything after second occurence
You're quantifying the group http://
twice instead of skipping to the second occurence. Use this regular expression:
/^(?:http:\/\/(.+)){2}/
Here is a regex demo!
How do I extract the nth occurence of a regex match from a string?
You may use three capturing groups separated with .*?
pattern that matches any 0 or more chars other than line break chars, as few as possible:
=REGEXEXTRACT(A1, "([A-Za-z0-9_]+\.(?:png|jpg)).*?([A-Za-z0-9_]+\.(?:png|jpg)).*?([A-Za-z0-9_]+\.(?:png|jpg))")
See the regex demo.
Details
([A-Za-z0-9_]+\.(?:png|jpg))
- a capturing group matching 1 or more letters, digits or underscores,.
, and then eitherpng
orjpg
.*?
- matches 0 or more chars other than line break chars, as few as possible.
If the second and third file names are optional, wrap the .*?
and the file pattern with an optional non-capturing group:
"([A-Za-z0-9_]+\.(?:png|jpg))(?:.*?([A-Za-z0-9_]+\.(?:png|jpg)))?(?:.*?([A-Za-z0-9_]+\.(?:png|jpg)))?"
Recap
To get the first match, just use
=REGEXEXTRACT(A1, "[A-Za-z0-9_]+\.(?:png|jpg)")
To get the second, use
=REGEXEXTRACT(A1, "(?:.*?([A-Za-z0-9_]+\.(?:png|jpg))){2}")
To get the third, use
=REGEXEXTRACT(A1, "(?:.*?([A-Za-z0-9_]+\.(?:png|jpg))){3}")
Related Topics
Arraylist: How Does the Size Increase
Java.Lang.Noclassdeffounderror: Org/Springframework/Core/Env/Configurableenvironment
Java.Exe Finished With Non-Zero Exit Value 1
Loop Over All Fields in a Java Class
Empty Check With String[] Array
Iterating Over Two Arrays Simultaneously Using for Each Loop in Java
How to Import Two Classes With the Same Name in Different Packages
Getting 400 for Spring Resttemplate Post
How to Handle Incorrect User Input into a Scanner
I Want to Detect the End of Scroll Event
Showing Morning, Afternoon, Evening, Night Message Based on Time in Java
Error Creating Bean With Name and Singleton Bean Creation Not Allowed
Java Sorting a 2D Array Rows into Ascending and Columns into Descending Order
Getresourceasstream Returns Null When Reading Properties File