How to make a look-Behind using quantifiers such as the " d+"?

Asked

Viewed 199 times

2

I need to match the "test" text, but the string will not always start with a fixed number of characters/digits, it may start with any number of characters, example:

001 test

0002 test

20458 test

How do I get the regexp below to work?

(?<=^\d+)test

That look-Behind (?<=^\d+) does not work, gives invalid expression, does not let use the quantifier d+ inside the look-Behind, if I do (?<= d d d d) it gives match only in the first situation. I wish it could be "variable" the number of digits/characters before the match text.

There is how to create a Behind look using +, * or {1 quantifiers,}?

  • Which language, if I’m not mistaken javascript has no lookbehind;;;

  • In fact, closing operators are invalid in lookbehind.

  • do not think it is JS, in the question itself he states that the lookbehind worked when changing the d+

  • JAVA / C#language, in javascript I am aware that there is no lookbehind.

3 answers

6

Instead of using lookbehind, you are best served using a capture group to get a reference to the sequence test:

/^\d+(test)/

Then you access group 1 to get the string test. All right it’s kind of pointless, being a string fixed, but the concept is the same when one wants to extract part of a string of characters.

4


The Response of @Wtrmute this in accordance with your need, but let us focus on your doubt.

How to make a look-Behind using quantifiers?

Answer

You don’t do, the look-Behind would have the logic that you know what comes before of his real capture.
And a quantifier breaks this rule, because if you use a quantifier it is precisely because you do not know how many times "something" should occur.

Addendum

You must remember that the look-Behind sure works backwards.

He will first look for teste after he "walks back" checking the look-Behind.

0002 teste
   ^^|---| captura
   ||- 1ª verificação do look-behind
   |- 2ª verificação do look-behind
  • I understand, well, I will mark this answer as accepted, since unfortunately there is no way to create a regex with dynamic look-Behind with quantifiers and what I want to do is not possible.

2

Regex without lookbehind (remembering that this way you need to access the group1 to get only "test".)

\d*? (teste)

Using lookbehind (this lookbehind identifies the use of 1 digit and space before the word "test", thus capturing independent of the number of digits.)

(?<=\d\s)teste
  • I think I was unhappy in formulating the question, I could not explain properly what I needed, in my example I have only digits before the "test" string, however, there are occurrences of any character, and sometimes whole sentences before my final goal, but I’ve been reading and I think there’s really no way to use a quantifier in the look.

  • It depends on the regex’s Flavour, where are you using it? Can I adjust my answer to suit your need

  • @Thiagohencke . s(test) solves? o . means any character that is not a line break, s indicates space before testing, then () captures what is between it if it matches

Browser other questions tagged

You are not signed in. Login or sign up in order to post.