Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match multiple occurances IFF another string occurs

Tags:

java

regex

I'm really hoping this can be solved in regex, but I fear not....

I'm looking for a regex that will return multiple matches of a term ONLY is another term appears in the same string. This is better explained with an example. Consider:

The numbers are 144, 424, and 345. Not 45.

I'd like to match '144', '424' and '345' only. (Any 3 digit number) - but only if they follow the term 'numbers' somewhere before. So the following aditional example:

The numbers we are looking for: 234 & 992

Should return '234' and '992' only.

The following sentence should not match anything:

Some examples: 234, 244 and 12

I thought I was onto something with the following regex:

(?<=numbers\b)(?:.|\n)*?\b(\d{3})\b

But it only matches the first number. Is what I'm trying to achieve even possible? No manner of lookahead or lookbehind seems to work here. For verious reasons I'm limited to this only being a single regex expression, and I don't have the option to selectivly access individual capturing groups after the fact. So looking for a purely regex method!

like image 321
Ben Heymink Avatar asked Jun 04 '19 20:06

Ben Heymink


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do you find how many occurrences of a regex pattern were replaced in a string?

To count a regex pattern multiple times in a given string, use the method len(re. findall(pattern, string)) that returns the number of matching substrings or len([*re. finditer(pattern, text)]) that unpacks all matching substrings into a list and returns the length of it as well.

What is multiline regex?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

What is a capture group regex?

Advertisements. Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".

What happens if no match is found in a string?

If a match is found, it will return the index of the first match between the regular expression and the provided string, or -1 if no match is found. const paragraph = 'The swift brown fox leaps over the sluggish dog.

How do you match characters between two characters in a string?

A regular expression that matches all characters between two specified characters makes use of look-ahead (?=…) and look-behind (?<=…) statements to isolate the string, and then uses the dot character . to select all contents between the delimiters.

How to iterate through multiple matches in a string in Python?

Internally, exec () may be used to iterate through multiple matches in a text string (with capture groups). const regex1 = RegExp('hello*', 'g'); const str1 = 'table hello, hello world'; let array1; while ((array1 = regex1.exec(str1)) !== null) { console.log(`Found $ {array1[0]}.

What is a regular expression?

A regular expression To match one of two or more words in a string. I Like regex. I Like pattern. I Like regex and pattern. I Like pattern and regex. I Like Regular Expression. Ignore case sensitive. Allows global search. Allows multiline search.


1 Answers

You may use this regex with \G:

(?:\bnumbers\b|(?!^)\G).*?\b(\d{3})\b

RegEx Demo

  • \G asserts position at the end of the previous match or the start of the string for the first match.
  • (?!^) avoids matching \G at line start
like image 59
anubhava Avatar answered Oct 21 '22 01:10

anubhava