Hows performs the regex: /.+? /?

How would ". +?" regular expression work? Is the part. + Coinciding with anything written, huh? part says it can be or not? So, for example, this regular expression will match:

'cat'
'' (i.e. nothing is written, just an empty string)

+3
source share
5 answers

In addition to what Hans Kesting has already said, the lazy multiplier will make an exact oposite from normal greedy factors: the possible match is kept as little as possible, and the rest of the regular expression is checked.

So, if you have a string aabaand check for a regular expression on it a.*b, the internal processing steps will be as follows:

  • ain corresponds a .*b a aba
  • .*in matches as well as greedy a .* ba a ba.*
    1. .*then matches a ab a
    2. .*then matches a aba
bin fails because there is no letter a.* b
  • backtracking goes one step back, and .*now it will only match bbina ab a
bin still not workinga.* baab a
  • backtracking goes one step back, and .*now only matches bina a ba
bin now matches in and have been met. a.* bbaa b a

So a complete match . aab a

(a.*?b), oposite, :

  • a a .*?b a aba
  • .* a .* ?b (*= ), .* (.*?),
  • b a.*? b a a ba
    • backtracking .*
  • .* a a ba
  • b a.*? b aa b a .

, , aab a.

+8

"+?" "+", "?" . "?" "+", "" " " , , .

, "a +?" regex "a" "caaat".

+10

+? ( )

. , , .

/".+?"/ "def" ( "ghi" ) abc "def" "ghi" jkl, /".+"/ "def" "ghi" .

+4

, Perl perldoc perlre.

"", .. ( ), . , , "?". , , "":

    *?     Match 0 or more times, not greedily
    +?     Match 1 or more times, not greedily
    ??     Match 0 or 1 time, not greedily
    {n}?   Match exactly n times, not greedily
    {n,}?  Match at least n times, not greedily
    {n,m}? Match at least n but not more than m times, not greedily
, , Perl . . , Perl "" .

    *+     Match 0 or more times and give nothing back
    ++     Match 1 or more times and give nothing back
    ?+     Match 0 or 1 time and give nothing back
    {n}+   Match exactly n times and give nothing back (redundant)
    {n,}+  Match at least n times and give nothing back
    {n,m}+ Match at least n but not more than m times and give nothing back
,

   'aaaa' =~ /a++a/
, a++ a . , perl , . , " " :

   /"(?:[^"\\]++|\\.)*+"/
, , . . (?>...); - . , :

   /"(?>(?:(?>[^"\\]+)|\\.)*)"/

+1

inevitably, the regular expression will search for at least one character. I came across a case where an empty line did not pass this test already, it would be better to use it .*?or (.*)?instead, sometimes you need to specify a part of the line, which can be zero in curly brackets before the question mark, this helps. For example. \d{6}?will lead to an incorrect result, whereas if I said (\d{6})?in a line, say, for example:

preg_match("/shu\.(\d{6})?/", "shu.321456")

this will give true, and so will be a string "shu."without any int after period

0
source

Source: https://habr.com/ru/post/1715319/


All Articles