Jump to content

sign ? after kvantifikator ( in regexp)

- - - - -

  • Please log in to reply
4 replies to this topic

#1
dadli

dadli

    Newbie

  • Members
  • PipPip
  • 12 posts
hello programmers :) my questions is about regexp.
this kvantifikator {4,7}? is - greater or equal 4 and lower 7. this is correct yes? if yes - why displays this code: "match is"

$str = "ddddddd";

 

if (preg_match("#^[d]{4,7}?$#",$str)) {

        echo "match is";

}


in variable $str symbol d is exactly 7 times

Thanks :)

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
nope, both numbers are inclusive - meaning 7 d's is allowed.

#3
dadli

dadli

    Newbie

  • Members
  • PipPip
  • 12 posts
both numbers are inclusive if kvantifikator is such a {4,7}
but if after kvantifikator is symbol ? kvantifikator {4,7}? already is greater or equal 4 and lower 7
I was wrong ???

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
No, adding a questin mark after something that has a variable occurence (like {x,y} or * or + etc) will just cause that to match using as few characters as possible.
This is easiest to test with the following

input: DDDDD
regex: (D{1,3}?)(.*)
result:

group1: D
group2: DDDD



input: DDDDD
regex: (D{1,3})(.*)
result:

group1: DDD
group2: DD


So as you see, by default each part of the regex tries to match as many characters as possible. The "?" basically tells the regex to try match with as few as possible.

#5
dadli

dadli

    Newbie

  • Members
  • PipPip
  • 12 posts
Thanks very much :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users