Regex to match many times

Simon H

I'm trying to match a type definition

def euro : t1 -> t2 -> t3 (and this pattern my repeat further in other examples)

I came up with this regex

^def ([^\s]*)\s:\s([^\s]*)(\s->\s[^\s]*)*

But while it matches euro and t1 it

  • then matches -> t2 rather than t2
  • fails to match anything with t3

I can't see what I am doing wrong, and my goal is to capture

euro t1 t2 t3

as four separate items, and what I currently get is

0: "def euro : t1 -> t2 -> t3"
1: "euro"
2: "t1"
3: " -> t3"
Wiktor Stribiżew

You can't use a repeated capturing group in JS regex, all but the last values will be "dropped", re-written upon each subsequent iteration.

When creating a regular expression that needs a capturing group to grab part of the text matched, a common mistake is to repeat the capturing group instead of capturing a repeated group. The difference is that the repeated capturing group will capture only the last iteration, while a group capturing another group that's repeated will capture all iterations.

The way out can be capturing the whole substring and then split it. Here is an example:

var s = "def euro : t1 -> t2 -> t3";
var rx = /^def (\S*)\s:\s(\S*)((?:\s->\s\S*)*)/;
var res = [];
var m = s.match(rx);
if (m) {
  res = [m[1], m[2]];
  for (var s of m[3].split(" -> ").filter(Boolean)) {
     res.push(s);
  }
}
console.log(res);

Pattern details

  • ^ - start of string
  • def - a literal substring
  • (\S*) - Capturing group 1: 0+ non-whitespace chars
  • \s:\s - a : enclosed with single whitespaces
  • (\S*) - Capturing group 2: 0+ non-whitespace chars
    • ((?:\s->\s\S*)*) - Capturing group 3: 0+ repetitions of the following pattern sequences:
    • \s->\s - whitespace, ->, whitespace
    • \S* - 0+ non-whitespace chars

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex: match many words multiple times

From Dev

How to use a regex to match if any pattern appears once out of many times in a given sequence

From Dev

Match regex pattern multiple times

From Dev

Regex to match n times for helm

From Dev

Ruby Regex match multiple times

From Dev

Regex match as many of strings as possible

From Dev

Match quantified word character as many times in a group

From Dev

Regex: match multiple times between tags

From Dev

regex match if dash is within string for n times

From Dev

Regex match multiple same expression multiple times

From Dev

Match a pattern multiple times using regex

From Dev

Regex group match exactly n times

From Dev

Match regex multiple times in one std::string

From Dev

Regex match capture group multiple times

From Dev

How to match and replace n number of times with RegEx

From Dev

Regex Optionally match a pattern multiple times

From Dev

Python regex match a pattern for multiple times

From Dev

Regex match multiple times in one line

From Dev

Regex to match string 2 times but not 3

From Dev

Issues with regex match, too many matches

From Dev

.NET regex match returning too many elements

From Dev

Regex match matches one too many characters

From Dev

Regex: match multiple times within regex defined region (emacs)

From Dev

Regular expression to match a number followed by a symbol repeated that many times?

From Dev

Sort foreign keys by how many times they match with specified foreign key

From Java

Find out how many times a regex matches in a string in Python

From Dev

How to match a regex 1 to 3 times in a sed command?

From Dev

Regex to match group less than or equal to n times

From Dev

Python regex: match only if pattern is repeated n number of times

Related Related

  1. 1

    Regex: match many words multiple times

  2. 2

    How to use a regex to match if any pattern appears once out of many times in a given sequence

  3. 3

    Match regex pattern multiple times

  4. 4

    Regex to match n times for helm

  5. 5

    Ruby Regex match multiple times

  6. 6

    Regex match as many of strings as possible

  7. 7

    Match quantified word character as many times in a group

  8. 8

    Regex: match multiple times between tags

  9. 9

    regex match if dash is within string for n times

  10. 10

    Regex match multiple same expression multiple times

  11. 11

    Match a pattern multiple times using regex

  12. 12

    Regex group match exactly n times

  13. 13

    Match regex multiple times in one std::string

  14. 14

    Regex match capture group multiple times

  15. 15

    How to match and replace n number of times with RegEx

  16. 16

    Regex Optionally match a pattern multiple times

  17. 17

    Python regex match a pattern for multiple times

  18. 18

    Regex match multiple times in one line

  19. 19

    Regex to match string 2 times but not 3

  20. 20

    Issues with regex match, too many matches

  21. 21

    .NET regex match returning too many elements

  22. 22

    Regex match matches one too many characters

  23. 23

    Regex: match multiple times within regex defined region (emacs)

  24. 24

    Regular expression to match a number followed by a symbol repeated that many times?

  25. 25

    Sort foreign keys by how many times they match with specified foreign key

  26. 26

    Find out how many times a regex matches in a string in Python

  27. 27

    How to match a regex 1 to 3 times in a sed command?

  28. 28

    Regex to match group less than or equal to n times

  29. 29

    Python regex: match only if pattern is repeated n number of times

HotTag

Archive