Re: Combining 2 preg matches.



frizzle wrote:
$regex ='/^(?:[a-z]|[a-z][_\-][a-z])+$/';

...will do just fine.

equally so:
$regex ='/^(?:[a-z]+(?:[_\-][a-z]+))+$/';

Lookahead & -behind are unneccessary in this case, and this keep it
simple.
Good point. It doesn't make sense to use assertions when you'll
capture
the matches anyway.
Somehow, i believe Rik's solution, gave me problems ...

'/^(?:[a-z0-9]|[a-z0-9][_\-][a-z0-9])+$/'; gave problems.
'/^(?:[a-z0-9]|(?<=[a-z0-9])[-_](?=[a-z0-9]))+$/' didn't.

An example string that gave problems is:
really_a_made_up_string


Ah, forgot that in [a-z0-9][_\-][a-z0-9] the character on the right is
already matched, so it won't work as a start for the second _ in _a_....

This one should still be working though:
$regex ='/^(?:[a-z0-9]+(?:[_\-][a-z0-9]+)*)$/';

Grtz,
--
Rik Wasmus


.



Relevant Pages

  • Please explain these regexps: funny lookahead behaviour
    ... why do the regular expressions Z? ... string = 'a' ... Surely lookahead only changes whether the match ... Jeremy Henty ...
    (comp.lang.ruby)
  • Re: acquiring value of primary key from uploaded file
    ... frizzle wrote: ... That's because mysql_last_insert_idis not a PHP function. ... was unfortunately on the fritz. ... Rik Wasmus ...
    (comp.lang.php)
  • Re: Problem with global text search using Regular Expressions
    ... >> I am trying to perform a global search in a string using regular ... There 'ccgcc' occurs only ... It probably uses lookahead in place of a plain match. ...
    (comp.lang.perl.misc)
  • Re: [regexp] Changing lines NOT containing a pattern
    ... depending on what the overal conditions are. ... remove something following something else in a string." ... This would be more efficient to use this in combination with a lookahead. ...
    (comp.lang.perl.misc)
  • Re: Regexp
    ... The bit is the lookahead, and won't consume any of the string ... you are searching. ... The beautiful soup answers are a better bet though - they've already ... lookahead solution fits better for the little I have to do. ...
    (comp.lang.python)