0

I am trying to match on number of spaces lower or higher than a specific count.

Example pattern:

x( ){4,}|( ){,2}x

Example data, where it should only match first and last lines:

x  x
x   x
x    x

Incorrectly marked as duplicate. {}+.* quantifiers don't achieve the desired results. And the question is not about syntax.

Thor
  • 498
  • 1
  • 6
  • 15
  • It is a dupe, just use limiting quantifiers. In the original question, you only had `+`. A sample solution is `x {0,2}x|x {4,}x`, for example. See [this demo](https://regex101.com/r/bZz5r4/1). You may shorten it as `x {0,2}+(?: {2,})?x` if your regex engine supports possessive quantifiers ([demo](https://regex101.com/r/bZz5r4/2)). – Wiktor Stribiżew Nov 13 '18 at 17:37
  • 1
    This `{,2}` is not a valid quantifier syntax. Also, there is no quantifier construct that will give you from a->b range and also from d->e range in a single construct. It has to be an alternation of choices `x{0,2}|x{4,}` –  Nov 13 '18 at 17:40
  • @sln It depends on regex library. In Onigmo, [it is valid](http://rubular.com/r/Nb5tDRlSn8). In TRE, it will work in an unexpected way matching 0 to 3 chars. – Wiktor Stribiżew Nov 13 '18 at 17:43
  • Oh, well I don't use obscure engines that are not standardized as quantifiers are. It's a parsing issue is all. Engines are commited to do the least amount of work possible in that regard. –  Nov 13 '18 at 17:45
  • DOH, that's `x{4,}|x{0,2}` since left is first. –  Nov 13 '18 at 17:49
  • @WiktorStribiżew don't think the question marked about syntax can be a duplicate for actual usage. It's like just linking documentation and will not extend similar search results. Of course your solution works, dunno why I didn't think of that, thank you! – Thor Nov 13 '18 at 17:52
  • 1
    @Thor If anyone thinks it is not a dupe, the question will be reopened sooner or later. – Wiktor Stribiżew Nov 13 '18 at 21:21

0 Answers0