-1

Consider following line (like in a table of contents):

6.1.34.2    Some text

(there is a tab after the "2").

When searching for ^\d\+[.]\d\+[.]\d\+[^.] the line is selected (and colored from "6" to "4"), which IMHO is not correct due to the last dot in the testcase.

With ^\d\+[.]\d\+[.]\d\+\s the line is not selected (as expected).

My question is, what is wrong with the first regex?

3
  • 2
    look at the debuger in regex101. As you can see the engine backtracks after match 34 with \d+, letting go of the 4 and matching it with [^.] Commented Apr 17 at 8:10
  • I upvoted this question because I learned several things from analyzing it and it's comments and answers. I'm not to keen on regex's myself but it looks like the solution is to either add a second [^.] after the first one or to use a positive look ahead to assert Twink what follows the last \d\+ is a tab like the ?= grammar does. Can someone else shed light on that? I'll benefit and assume the poster will too. Appreciated. :) Commented Apr 17 at 14:17
  • @user377241 1) in general, please don't ask follow up questions in comments. Ask a new question instead or just try it yourself. Regex tools and documentation are freely available. 2) not sure what solution you're talking about. OP expects their regex to "not work" which is as sketchy as it gets. Commented Apr 18 at 8:29

1 Answer 1

2

\d\+[^.]

Here, \d\+ matches the 3, and [^.] matches the 4.

You seem to expect that \d\+ matches 34 and then [^.] will not match the .. I'm not sure if that expectation is reasonable (i.e., whether \d\+ has to be greedy).

Sign up to request clarification or add additional context in comments.

4 Comments

Just out of curiosity: Is vim using a special syntax flavor of regex? According to my understanding of basic regex syntax \d\+ should just match a single digit followed by a single '+' character, whereas \d+ is the pattern to match one or more sequential digits.
@Halfix yes, Vim has its own regex flavor. See :help pattern-overview.
:help \+ gives the description "\+ Matches 1 or more of the preceding atom, as many as possible." . With the pattern ^\d\+[.]\d\+[.]\d\+, it matches 6.1.34 while the last \d\+ matches 34. With the pattern ^\d\+[.]\d\+[.]\d\+[^.], it still matches 6.1.34 while the last \d\+ matches 3 only such that [^.] can match 4. Both cases conform to the rule "as many as possible".
I was reprimanded in the question comments for suggesting that the poster try this answer and or ask for help with a positive look ahead if he couldn't figure out how to get the line to match ..34[.] I'll ask for the sake of this thread, could you clarify in your answer whether the look ahead or additional [^.] would be best to ensure the whole index before tab character is matched, please? Many thx :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.