in reply to Regex Semantics

So, a regex that actually matched "A line beginning and ending with zero or more letters in the set/class {a}" would be /^[a]*.*[a]$/s: The beginning of the string has to be followed by zero or more a's, followed by zero or more things in the middle of the string, followed by zero or more a's before the end of the string (or before a newline preceding the end). But with that "zero" in there instead of "one" or more, you are only requiring that the string have a beginning and an end, which is true of all strings.

Replies are listed 'Best First'.
Re^2: Regex Semantics
by ikegami (Patriarch) on Oct 04, 2006 at 23:57 UTC

    So, a regex that actually matched "A line beginning and ending with zero or more letters in the set/class {a}" would be

    I didn't touch that in my post because both /^.*\z/s and 1 would be sufficient to match "A line beginning and ending with zero or more letters in the set/class {a}".

    If you wanted a regexp that matched "A line beginning and ending with one or more letters in the set/class {a}", you'd use
    /^[a]/ && /[a]\z/ (Two regexps),
    /^(?=[a])(?=.*[a]\z)/s (Lookaheads) or
    /^[a].*(?<=[a])\z)/s (One of the few times (?<=...) works.)

    /^[a].*[a]\z/s would not do, since it wouldn't match "a".