by Devin Yang
(This article was automatically translated.)

Published - 1 year ago ( Updated - 1 year ago )

Feel free to use Laravel's tinker to display the lookbehind function
He needs a fixed length, we can grab the text after the brackets

The original content is:

How fast is the car moving?
How far do you drive to work every day?

Through the formal lookbehind, only look at the words after How fast and How far

(?<=How\s\w{4}\s).+|(?<=How\s\w{3}\s).+

Laravel tinker screen

php artisan tinker
Psy Shell v0.11.8 (PHP 8.1.11 — cli) by Justin Hileman
>>> $re = '/(?<=How\s\w{4}\s).+|(?<=How\s\w{3}\s).+/m ';
=> "/(?<=How\s\w{4}\s).+|(?<=How\s\w{3}\s).+/m"

>>> $str = 'How fast is the car moving?
... How far do you drive to work every day?
...
... ';
=> """
   How fast is the car moving?\n
   How far do you drive to work every day?\n
   """

>>>
>>> preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
=> 2

>>>
>>> // Print the entire match result
>>> var_dump($matches);
array(2) {
  [0] =>
  array(1) {
    [0] =>
    string(18) "is the car moving?"
  }
  [1] =>
  array(1) {
    [0] =>
    string(31) "do you drive to work every day?"
  }
}

 

If you want to study the formal form, recommend the following is really "great".

https://regex101.com/r/dhGbvw /1

 

Tags: regular expression

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments