1

I'm trying to extract match patterns like:

AA/8G+8G+8G+8G/WITHOUT *

AA/8*G+8*G+8*G+8*G/WITH *

AA/8G+8*G+8*G+8*G/MIXED, THIS IS NOT SUPPORTED YET

using the following regular expression:

https://regex101.com/r/zemJ8H/1

but it matches only 8G+8G+8G+

because the pattern is identified as 8G**+**

Is there any way to include the last 8G (without +) in the group?

Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
Salvuccino
  • 73
  • 1
  • 5
  • I'm trying to extract the content between couples of / if a valid pattern is found. With "valid pattern" I mean combination of numbers (1 or 2 digits), followed by a letter (either M or G) and a + symbol or nothing (if it's the latest item of the sequence). So I'm expecting to obtain 8*G+8*G+8*G+8*G or 8G+8G+8G+8G in the list of matches. I tried with (?<=/)([0-9]{1,2}[\*]*[GM][\+]{0,1}?)\1+ but the last item of the sequence is missing. – Salvuccino Dec 15 '17 at 16:25

2 Answers2

0
AA/(8\*?G\+?)+/

https://regex101.com/r/zemJ8H/2

What about something like this? AA followed by a plan followed by any number of sequences containing an 8, optionally a *, a G , and optionally a following +, finally followed by a +.

erik258
  • 14,701
  • 2
  • 25
  • 31
  • To didn't have any examples in the regex 101 if any other strings to match, but if desired it would be an easy matter to change the 8 and G to character classes – erik258 Dec 15 '17 at 16:29
  • Thanks, but this is matching "too much" :) The solution I was looking for is the one provided by dasblinkenlight – Salvuccino Dec 15 '17 at 16:32
0

This expression takes care of all three cases, including the mixed one:

(?<=/)([0-9]{1,2})[*]*([GM])[+]?(\1[*]*\2[+]?)+

Demo

The idea is to separate out the capturing of digits (new \1) from letters (the \2) and use both captures in the repeating group (\1[\*]*\2[\+]?)+ at the end of the expression.

Regex groups

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Are there regex dialects that require backslash escapes of meta characters in square brackets? I would have thought `[*]`, not `[\*]` – erik258 Dec 15 '17 at 16:31
  • This is the expected solution! Thanks! How can I mark it as the solution? – Salvuccino Dec 15 '17 at 16:31
  • @DanFarrell You are right, I copy-pasted parts of OP's regex into my version. Backslashes are not required. I edited the answer, and updated the demo. Thank you very much! – Sergey Kalinichenko Dec 15 '17 at 16:38