Regex Match Multiple Times in String

Match a string pattern multiple times in the same line

Problem is that your regex (\{\$.+\$\}) is greedy in nature when you use .+ that's why it matches longest match between {$ and }$.

To fix the problem make your regex non-greedy:

(\{\$.+?\$\})

Or even better use negation regex:

(\{\$[^$]+\$\})

RegEx Demo

Regex match a pattern occurring multiple times in a string

You can use

^[0-9]+:[0-9]+, 80:[0-9]+, 443:[0-9]+(, [0-9]+:[0-9]+)+,$

See the regex demo.

Also, consider the awk solution like

awk '/^[0-9]+:[0-9]+(, [0-9]+:[0-9]+)+,$/ && /80/ && /443/' file

See the online demo:

#!/bin/bash
s='0:0, 80:3, 443:0, 8883:0, 9000:0, 9001:0,
0:0, 80:0, 443:1, 8883:0, 9000:0, 9001:0,
0:0, 80:0, 443:0, 8883:0, 9000:0, 9001:0,
0:0, 80:3, 443:1, 8883:0, 9000:0, 9001:0,
0:0, 443:0, 8883:0, 9000:0, 9001:0,
0:0, 80:0, 8883:0, 9000:0, 9001:0,
0:0, 8883:0, 9000:0, 9001:0,'
awk '/^[0-9]+:[0-9]+(, [0-9]+:[0-9]+)+,$/ && /80/ && /443/' <<< "$s"

Output:

0:0, 80:3, 443:0, 8883:0, 9000:0, 9001:0,
0:0, 80:0, 443:1, 8883:0, 9000:0, 9001:0,
0:0, 80:0, 443:0, 8883:0, 9000:0, 9001:0,
0:0, 80:3, 443:1, 8883:0, 9000:0, 9001:0,

RegEx Match multiple times in string

Use a positive look ahead and look behind assertion to match the angle brackets, use .*? to match the shortest possible sequence of characters between those brackets. Find all values by iterating the MatchCollection returned by the Matches() method.

Regex regex = new Regex("(?<=<<).*?(?=>>)");

foreach (Match match in regex.Matches(
"this is a test for <<bob>> who like <<books>>"))
{
Console.WriteLine(match.Value);
}

LiveDemo in DotNetFiddle

Regex match multiple same expression multiple times

Note you are using ^ anchor at the start and that makes your pattern only match at the start of a line (mind also the m modifier). .+?(?=\s|$) is too cumbersome, you want to match any 1+ chars up to the first whitespace or end of string, use {\S+ (or {\S* if you plan to match { without any non-whitespace chars after it).

You may use

s = s.replace(/{\S*|}/g, '')

You may trim the outcome to get rid of resulting leading/trailing spaces:

s = s.replace(/{\S*|}/g, '').trim()

See the regex demo and the regex graph:

Sample Image

Details

  • {\S* - { char followed with 0 or more non-whitespace characters
  • | - or
  • } - a } char.

Regex match capture group multiple times

Use

(?:x|(?<!\A)\G).*?\Kb(?=.*z)

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
x 'x'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\A the beginning of the string
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
\G where the last m//g left off
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
\K match reset operator (omits matched text)
--------------------------------------------------------------------------------
b 'b'
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except line breaks (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
z 'z'
--------------------------------------------------------------------------------
) end of look-ahead

Match a pattern multiple times

Use the global option, when matching multiple times, plus "m" flag for multiline

(^%(?!\s)[^%\n]*$)/gm

http://www.regexpal.com/?fam=97259

Regex match multiple times in one line

If greedy is the problem, you can try using \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}[^(]+?Additional comments. ? after + turns it into non-greedy, https://docs.python.org/3.7/library/re.html#regular-expression-syntax.

I am not quite sure what output is required, and I would suggest to format the question so that spaces and newlines are clear. Include the current output and required output as well.

Regex: match multiple times between tags

You may try this:

def(?=[^<>]*?<\/)

Explanation:

  1. def matches def
  2. (?=[^<>]*<\/) Positive look ahead that is looking for a </ i.e.
    end tag without matching < and > before it [^<>]*?

Example



Related Topics



Leave a reply



Submit