How can I extract more than 1 match of the same pattern with regular expression?

regular expression match
python regex
regex match pattern multiple times
regex r
pcre regex
javascript regex matcher
sed regex tester
python online regex

For example, if I have 2 strings

[Mandzukic 18' (o.g.) Griezmann 38' (pen.) Pogba 59' Mbappe 65'; Perisic 28' Mandzukic 69']

and

[Samuel Umtiti 51']

How can I write a single regex that can extract [Mandzukic, 18, Griezmann, 38, Pogba, 59, Mbappe, 65, Perisic, 28, Mandzukic, 69] from the first string and [Samuel Umtiti, 51] from the second string?

This is what I have so far: (\w\s*\w+)\s(\d+) but I don't know how to get it to extract more than 1 instance of this pattern

May be you this help,

    import re
    st = "Mandzukic 18' (o.g.) Griezmann 38' (pen.) Pogba 59' Mbappe 65'; Perisic 28' Mandzukic 69'"
    re.findall(r'(\w\s*\w+)\s(\d+)', st)
    # Output as: [('Mandzukic', '18'),
                  ('Griezmann', '38'),
                  ('Pogba', '59'),
                  ('Mbappe', '65'),
                  ('Perisic', '28'),
                  ('Mandzukic', '69')]

Quantifiers in Regular Expressions, What is the regular expression matching zero or more specific characters? The first regular expression tries to match this pattern between zero and two times; the second, exactly two times. Because the first pattern reaches its minimum number of captures with its first capture of String.Empty, it never repeats to try to match a\1; the {0,2} quantifier allows only empty matches in the last iteration.

You can use re.findall to find all the matches of a pattern. As for optionally matching a second word in your second sample input, you can put the word in a group with a ? to make it optional:

import re
for s in "Mandzukic 18' (o.g.) Griezmann 38' (pen.) Pogba 59' Mbappe 65'; Perisic 28' Mandzukic 69", "Samuel Umtiti 51'":
    print(re.findall(r'(?:\w+\s+)?\w+\s+\d+', s))

This outputs:

['Mandzukic 18', 'Griezmann 38', 'Pogba 59', 'Mbappe 65', 'Perisic 28', 'Mandzukic 69']
['Samuel Umtiti 51']

Regular Expression (Regex) Tutorial, How do I extract a string from a normal expression in Python? Just for the record I needed the same thing, but in case there were more than one hyphen I had to make sure I got the text at the end after the last " - ". After reading a few blog posts on the topic, I used the following regular expression as the pattern to extract the text and it worked perfectly: (?<=\w+\s-\s)\w+. Hope this helps someone.

s1="[Mandzukic 18' (o.g.) Griezmann 38' (pen.) Pogba 59' Mbappe 65'; Perisic
 28' Mandzukic 69']"
import re
re.findall('(\w\s*\w+)\s(\d+)', s1)
[('Mandzukic', '18'), ('Griezmann', '38'), ('Pogba', '59'), ('Mbappe', '65'), ('
Perisic', '28'), ('Mandzukic', '69')]

Regular-Expressions.info, ( re ) module. >>> s = '/tmp/10508. constantstring' >>> s. split('/tmp/')[1]. One part of the expression matches strings such as '+a', '-57' etc. A + or a - followed by any number of letters or numbers. I want to match 0 or more strings matching this pattern. This is the expression I came up with: ([\+-][a-zA-Z0-9]+)* If I were to search the string '-56+a' using this pattern I would expect to get two matches: +a and -56

How to extract the substring between two markers?, Alternatively, expand your regex and let the pattern contain one to get three groups in one match rather than three matches using one group. The method scan looks for each bit of text that matches the given pattern (regular expression). It then extracts all the “marked” (captured) parts from it, and keeps these as an array. As there can be many occurances that match the pattern, and each of them can have many captures, we get back a nested array.

How to capture multiple repeated groups?, We will show some examples of how to use regular expression to extract and/or Example 1: A researcher has addresses as a string variable and wants to create a variable that contains full names in the order of first name and then last name. For example if I wanted to match a number made up of one or more digits if  More generally, to extract the first match of a regexp, you need to compute the negation of that regexp. While this is always theoretically possible, the size of the negation grows exponentially with the size of the regexp you're negating, so this is often impractical. Consider your other example: sed -n 's/.*\(CONFIG_[a-zA-Z0-9_]*\).*/\1/p'

How can I extract a portion of a string variable using regular , Allows you to extract values using regular expression syntax to parse more For more information on Regular Expressions, see the Perl The field=fieldname option allows you to specify a field to parse other than the default message. include messages that do not match any segment of the parse term. An OR clause for regular expressions. Instead of using OR to chain multiple REGEXP_LIKE functions together when there's more than one possible search pattern, the alternation operator, which is the pipe symbol |, can combine those search patterns together as options within a single regex.

Comments
  • how does you call your method for search?
  • someVariable = re.search("(\w\s*\w+)\s(\d+)", stringName)
  • Thanks, this was exactly what I needed!