How to do a look-behind using quantifiers like "\ d +"?

1

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

001 test

0002 test

20458 test

How do I make the regexp below work?

(?

asked by anonymous 24.05.2017 / 21:21

3 answers

3

The @Wtrmute answer is in line with your need, but let's focus on your question.

  

How to do a look-behind using quantifiers?

Response

You do not , the look-behind would have the logic that you know what comes before from your actual capture. And a quantifier breaks this rule, because if you use a quantifier it's just because you do not know for sure how many times something should happen.

Addendum

You should remember that look-behind sure works backwards.

He will first look for teste after he "walks around" by checking look-behind .

0002 teste
   ^^|---| captura
   ||- 1ª verificação do look-behind
   |- 2ª verificação do look-behind
    
26.05.2017 / 14:59
6

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

/^\d+(test)/

Then you access group 1 to get the string test . It's okay that it's kind of useless if it's a fixed string , but the concept is the same when you want to extract part of a string.

    
24.05.2017 / 22:51
2

RegEx without lookbehind (remembering that so you need to access group1 to get only "test".)

\d*? (teste)

Using lookbehind (this lookbehind identifies the use of 1 digit and space before the word "test", so it would capture regardless of the number of digits.)

(?<=\d\s)teste
    
24.05.2017 / 23:37