0

aaa[image,L,0]bbb [1]ddddffffdddd[image,R,0]ccc

When there is a String of this type, I used the following regular expression to extract only the value of the form [image,L,0].

\[.*?[^\]]\,[LMR]\,.*?[^\[]\]

In the above String, I wanted the result to be [image,L,0],[image,R,0]. However, the actual extracted values ​​were [image,L,0],[1]ddddffffdddd[image,R,0]. I want to extract only the values ​​with two , in square brackets. I want to know what am I missing in my regex.

extradeck
  • 366
  • 1
  • 5
  • 19
  • 1
    The dot will skip over any closing `]`. You can get these by using [negation](https://www.regular-expressions.info/charclass.html#negated): [`\[[^\]\[]*\,[LMR]\,[^\[\]]*\]`](https://regex101.com/r/nSpJel/1) – bobble bubble May 13 '22 at 00:46
  • 1
    Welcome! If there can occur more than two commas inside the square brackets and you only want those with two at most, you need to [add them to the negated class too](https://regex101.com/r/YSkCvB/1). – bobble bubble May 13 '22 at 01:24
  • [This](https://regex101.com/r/rfxaKS/1) will work. – Wiktor Stribiżew May 13 '22 at 08:34

0 Answers0