0

what does non capturing group does inside a look ahead (?=......(?:......))

  • 3
    In **your** example it does absolutely nothing except add processing overhead. – MonkeyZeus Jun 22 '21 at 14:41
  • Non-capturing groups are most often used to avoid the processing overhead of creating backreferences/capture groups. – MonkeyZeus Jun 22 '21 at 14:43
  • sorry for that example, actually i meant this syntax (?!.*(?:\\.\\.)) – user16273861 Jun 22 '21 at 14:44
  • 1
    It does nothing useful in that example either. – MonkeyZeus Jun 22 '21 at 14:45
  • 1
    Test out the regex with/without the non-capture group in real-time at https://regex101.com/ and pay attention to the step counter in the upper right. – MonkeyZeus Jun 22 '21 at 14:48
  • @MonkeyZeus It does not necessarily change the number of steps. See my regex demo in my answer and then change the regex to be `(\d+)(?=(?:b|c))` and the number of steps will not change. – Booboo Jun 22 '21 at 15:07
  • 1
    @Booboo I beg to differ. Every situation is different and will be affected by both the regex and the input string https://regex101.com/r/25VOP5/1 versus https://regex101.com/r/6ixrVZ/1. Once again, there is zero syntactical justification for OP's examples and ditto for your example. Since you cannot control the input string you should strive to make your regex efficient. Sometimes, readability is a reasonable tradeoff for efficiency but that's outside this scope. – MonkeyZeus Jun 22 '21 at 15:14

1 Answers1

1

It does the same thing it does outside of a lookahead.

Consider the following regex:

(\d+)(?=(b|c))

And searching the string '123c'

See regex demo

For example, in Python:

import re

m = re.search(r'(\d+)(?=(b|c))', '123c')
print(m.group(1), m.group(2))

Prints:

123 c

But with ...

(\d+)(?=(?:b|c))

... there is only capture group 1.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
Booboo
  • 38,656
  • 3
  • 37
  • 60
  • Thank you so much for your answer , but I'm still confused, how this ^(?!.*(?:\\.\\.))[a-zA-Z0-9_'.-]+@[a-zA-Z0-9_.-]+\\.[a-zA-Z0-9&]+$ syntax is returning false while there is multiple dot in input. – user16273861 Jun 22 '21 at 15:19
  • I need at least 15 reputation to cast a vote, sorry. – user16273861 Jun 22 '21 at 15:21
  • You don't need any reputation to *accept* an answer. Read the link. And if you are asking now a *new* question, you should post it as such with the language you are using and **the input string you are trying to match** and what the expected result should be and what you are actually getting. But don't "piggy-back" on this question. – Booboo Jun 22 '21 at 15:25
  • I think you have the double backslashes followed by a period, i.e. `\\.` ,within a string literal where you are required to escape the backslash with an extra backslash but ultimately what you are *trying* to express if simply `\.`, which is to escape the period so it is treated as a literal period rather than a wildcard character. In that case your negative lookahead assertion just says that the input cannot have any two consecutive periods. Now if you are not trying to accomplish that, that is a different story. This is why you need to post your code as I have indicated. – Booboo Jun 22 '21 at 15:37
  • @user16273861 Ditto to what Booboo said. This wild goose chase is quite possibly the most annoying thing you can do on this site. Present your regex and explain what strings you expect it to match or fail to match. – MonkeyZeus Jun 22 '21 at 15:38
  • @Booboo Sorry, actually this is the first question I'm asking on this site, I'm trying to match an email using that pattern in java. – user16273861 Jun 22 '21 at 15:42
  • I will say this again: **Post a new question tagged with `java`. Explain in English what you are trying to match. Then show the specific string(s) you are matching against and what the expected results. Show your code!!!** And i fyou don't want to accept this answer, you are under no obligation to. – Booboo Jun 22 '21 at 15:52
  • @user16273861 So in plain English you're trying to prevent the username from having 2 consecutive periods? Because that's what your regex **is** doing. If you don't want it doing that then remove `(?!.*(?:\\.\\.))` – MonkeyZeus Jun 22 '21 at 16:00
  • @MonkeyZeus I just want to how (?!.*(?:\\.\\.)) is working – user16273861 Jun 22 '21 at 16:02
  • 1
    That is 100% different from your current post. You should post a new question and explain what you do understand versus what you don't understand about `^(?!.*(?:\\.\\.))[a-zA-Z0-9_'.-]+@[a-zA-Z0-9_.-]+\\.[a-zA-Z0-9&]+$`. Rule #1, don't make others force the real question out of you. This comment thread is nothing short of exasperating. – MonkeyZeus Jun 22 '21 at 16:18
  • @MonkeyZeus sorry. – user16273861 Jun 22 '21 at 16:43
  • Ad an addition, there is no need for an extra grouping in the lookahead, this will be enough without any groups `\d+(?=b|c)` – The fourth bird Jun 22 '21 at 18:56