0

I've been referring to http://www.regular-expressions.info/reference.html, however I just cannot understand how my regex unable to success.

function is_name($Argv){
    $RegExp='/^[a-zA-Z]+[a-zA-Z()\. ]?+[a-zA-Z)]?+$/';
    return preg_match($RegExp,$Argv)?$Argv:false;
}

I'm trying to make the name alphabet start, optional for some symbol or space at middle and ')' available at the end.

$testArray=array('L','D Luffy','Luffy','Monkey D. Luffy','Choppa(fiftyB)');
foreach($testArray as $key=>$value)
echo $auth->check->is_name($value); //output 'LLuffy'

it turns out only 'L' and 'Luffy' acceptable but not matching middle space which is optional. I can't opt out the middle optional as I have to match for 'L'.


update

problem solved, its the problem that i'm trying to do []? however( I think ) the php matching ?+ before do []?

function is_name($Argv){
    $RegExp='/^[a-zA-Z]+([a-zA-Z()\. ]?)+([a-zA-Z)]?)+$/';
    return preg_match($RegExp,$Argv)?$Argv:false;
}
Anonymous
  • 1,155
  • 4
  • 13
  • 25

2 Answers2

0

The expression isn't right,

/^[a-zA-Z]+[a-zA-Z()\. ]?+[a-zA-Z)]?+$/

You are saying: Start of string followed by a letter (any case) one or more times, followed by an (optional letter bracket dot or space) one or more times, followed by an (optional letter or bracket) one or more times then the end of the string.

Not quite sure how it gets LLuffy, but the ?+ are just ... pointless, comment back or edit with what you are trying to match and I'll tell you.

Alec Teal
  • 5,371
  • 3
  • 16
  • 48
  • 1
    `?` means match the precedent token *zero* or *one* time, adding a plus to it `?+` is different, the `+` in this case is a *possessive quantifier*. A possessive quantifier is useful for performance, it will prevent the engine from trying to backtrack different permutations. How ironic to hear this from someone who has to finish elementary school (according to you) :-) – HamZa Aug 27 '13 at 06:18
  • @HamZa I have no idea what you're on about, +? is a lazy match, + is by default greedy. ? is 1 or 0 times, IE optional. – Alec Teal Aug 27 '13 at 14:19
  • 1
    You mentioned `followed by an (optional letter bracket dot or space) one or more times` "__one or more times__" which is not true. Learn about possessive quantifiers from [here](http://stackoverflow.com/a/5319978) and [here](http://www.regular-expressions.info/possessive.html). – HamZa Aug 27 '13 at 14:59
  • @HamZa not in POSIX regex there's not, read about finite state automatons it can be show that all regex rules map to state machines, but not the other way, PERL things and other extras alter the state machine, see Flex (Fast Lexical Analyser) for the improvements of state machines over Perl's interpretation, I do approve of Perl's regex, but only when stated in the question. – Alec Teal Aug 27 '13 at 15:17
  • I'm not stalking, it just happened that you answered a regex/php question which I frequent a lot. But I'm done here. – HamZa Aug 27 '13 at 15:45
0

problem solved, its the problem that i'm trying to do []? however( I think ) the php matching ?+ before do []?

Yes, since the PHP documentation says

Repetition is specified by quantifiers, which can follow any of the following items:

  • a single character, possibly escaped
  • the . metacharacter
  • a character class
  • a back reference (see next section)
  • a parenthesized subpattern (unless it is an assertion - see below)

+ as a quantifier cannot follow the repetition [...]?, because this is none of the items above. Rather (as HamZa correctly stated) ?+ is interpreted according to this:

Quantifiers followed by + are "possessive". They eat as many characters as possible and don't return to match the rest of the pattern.

Thus, with the pattern /^[a-zA-Z]+[a-zA-Z()\. ]?+[a-zA-Z)]?+$/ and the subject D Luffy, [a-zA-Z]+ matches D, [a-zA-Z()\. ]?+ matches the space, and [a-zA-Z)]?+ matches just the L, not the whole Luffy.

Armali
  • 14,228
  • 13
  • 47
  • 141