11

I have the following regex:

"(.+?)",.+?},\s.+?:\s

I would like to know if there is a way to repeat this regex, so i don't need to write it several times like this:

"(.+?)",.+?},\s.+?:\s"(.+?)",.+?},\s.+?:\s"(.+?)",.+?},\s.+?:\s
o1-steve
  • 321
  • 1
  • 4
  • 11
  • Just use string blocks to build the pattern dynamically. – Wiktor Stribiżew May 12 '16 at 18:13
  • 2
    Use the `/g` flag? – CinCout May 12 '16 at 18:18
  • As @torazaburo say's, quantify the constructs within a cluster group `(?:"(.+?)",.+?},\s.+?:\s){3}` However, that means the single capture group will be overwritten 3 times, containing the final value of the 3rd quantified pass. –  May 12 '16 at 19:01
  • 1
    Suspiciously, you have quantifiers and a group in the regex, yet you still ask if there is a way to quantify that block 3 times. You'd think this is a question that would come up before one knew how to use groups and quantifiers, not the reverse.. –  May 12 '16 at 19:09

1 Answers1

26

Put the regEx block in () and add * or +.

* 0 to any number of times.

+ 1 to any number of times.

{n} 'n' times.

{n,} at-least 'n' times.

(?: ... ) is called non-capturing group

Non-capturing parentheses group the regex so that you can apply regex operators, but do not capture anything.

Eg:

[0-9]{1} this means 1 digit(0-9)

[0-9]+ this means at-least one digit(0-9).

[0-9]* no digits or any number of digits(0-9).


Since you wanted "(.+?)",.+?},\s.+?:\s"(.+?)",.+?},\s.+?:\s"(.+?)",.+?},\s.+?:\s,

you may do it like this : ("(.+?)",.+?},\s.+?:\s){3}.

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • 1
    And if necessary use `(:` to avoid capturing. –  May 12 '16 at 18:17
  • 1
    The block could also be appended with `{3}` if the OP is looking for exactly 3 matches. – lurker May 12 '16 at 18:21
  • 3
    wouldn't that be `(?:` to avoid capturing? – Nick May 12 '16 at 18:25
  • @Nick yes, that was probably a typo by [tarozaburo](http://stackoverflow.com/users/663031/torazaburo) – Ani Menon May 12 '16 at 18:33
  • @lurker yes, covered :) – Ani Menon May 12 '16 at 18:34
  • You could also use the recursive option `(?R)?` at the end of your existing regex, if your engine supports it. – James Buck May 12 '16 at 18:44
  • @JamesBuck Example : The regexes `a(?R)?z`, `a(?0)?z`, and `a\g<0>?z` all match one or more letters `a` followed by exactly the same number of letters `z`. [Reference link](http://www.regular-expressions.info/recurse.html) – Ani Menon May 12 '16 at 18:48
  • This is not the same. Using a quantifier on a fragment will not create more captured groups. OP's regex has 3 captured groups, but this approach will only have 1 captured group. – Bohemian May 12 '16 at 19:13
  • @Bohemian A variable number of capturing groups may not be used, but we can specify the number of times it is to be repeated. Eg:`(RegEx){3}` [Also check](http://stackoverflow.com/a/6939587/2142994) – Ani Menon May 12 '16 at 19:19
  • @Ani, but the OP is capturing 3 separate parts of the input, which your approach can not do – Bohemian May 12 '16 at 19:22
  • @Bohemian No he wants to repeat the same regEx multiple times. `"I would like to know if there is a way to repeat this regex"` – Ani Menon May 12 '16 at 19:24
  • @Ani no, he wants to create the larger regex as per his question by repeating a smaller regex, which can not be done due to there being 3 capturing groups in his desired final regex – Bohemian May 12 '16 at 19:26
  • Here, Bohemian is right, there are capturing groups. And that means the question is unclear and should be closed. – Wiktor Stribiżew May 12 '16 at 19:30
  • @WiktorStribiżew He wanted that pattern shown without repeating the smaller pattern, I have added the solution to that in the answer above. – Ani Menon May 12 '16 at 19:32
  • @Bohemian I think that is what he wanted as his question was very specific to "how to get this?" – Ani Menon May 12 '16 at 19:34
  • @ani and your answer does NOT achieve his stated goal. Your regex is not the same. It's very simple – Bohemian May 12 '16 at 19:42
  • @wik I don't think the question is unclear because OP has posted the exact regex he wants to achieve. – Bohemian May 12 '16 at 19:43