0

I get as input a line which specifies a sensor and its settings for several kind of thresholds or so that all are represented in the same format and appear after each other 3 times (and some other stuff after this which isn't the issue here). Using regex101 I wrote my pattern which looks something like this:

^([0-9]{2})\:([a-zA-Z0-9\.\s]{4})([\s]?[\s\-\+\.0-9]{0,6}[\s]{1}){3}([\°|\s][a-zA-Z]\s)([0-9\.\-\s]{6}\s)(E[\+\-]{1}[0-9]{1}\s)([0-9\.\-\+\s]{0,6}\s)([a-zA-Z0-9\s]{0,10})

As you see I want to match this 3rd group ([\s]?[\s\-\+\.0-9]{0,6}[\s]{1}){3} exactly three times - because I know it will appear three times. However regex101 tells me this:

A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data

So apparently it is not impossible to capture each single event of the group's repetition but trying to wrap brackets around the whole term doesn't seem to solve it either. Also in this SO comment it was mentioned that

there's no way to create a dynamic number of capture groups

which rather contradicts what the suggestion on regex101 and seems reasonable given that my attempt to solve it by the suggested surrounding group didn't work.

However, just to be sure I'd like to ask for clarification on this nevertheless because somehow it would also seem unreasonable to me to not have something that lets you catpure a repeating group a dynamic number of times. I suppose there should be at least one way to do this instead of making something like this:

([\s]?[\s\-\+\.0-9]{0,6}[\s]{1})([\s]?[\s\-\+\.0-9]{0,6}[\s]{1})([\s]?[\s\-\+\.0-9]{0,6}[\s]{1})

instead of e.g. something like this (which I know apparently doesn't work, I'm referring to the length of both versions however):

(([\s]?[\s\-\+\.0-9]{0,6}[\s]{1}){3})
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Samaranth
  • 385
  • 3
  • 16
  • The number of groups is set and then fixed by the pattern. Only if you are using a PyPi regex in Python, or .NET regex, you can access the *captures* themselves, not *group* values, to get the repeated capturing group values. Now, the question is what language you are using. You also seem to misunderstand what regex101.com tip tells you. The point is to extract all values in a single group, then you may split the group into different values. – Wiktor Stribiżew Jun 19 '20 at 08:18
  • This project is done in Python using `re` for regex pattern matching. Thanks for pointing out my missunderstanding of the tip from regex101. So what you point out in your last sentence is something like this: ( ( [\s]?[\s\-\+\.0-9]{0,6}[\s]{1} ) {3} ) which returns one value containing all three iterations of the pattern and the splitting it e.g. by some character like a ` ` (which doesn't make sense in this specific case but just to get the idea correct here)? – Samaranth Jun 19 '20 at 08:33
  • Yes, use `((?:\s?[-\s+.0-9]{0,6}\s){3})` and then when you get the match, split the value of this capturing group. Or, just get PyPi regex and use `.captures`. – Wiktor Stribiżew Jun 19 '20 at 08:35

0 Answers0