http://www.perlmonks.org?node_id=106200


in reply to Regexpresions

the first regex is going to look for a single character between '\' and '\', which is not quite what you want... this is because of the '?' quantifier after the '.'. '?' means one or more.

the second one should only produce a single character - the first char of 'some text'... the '[^\\]' has an implicit quantifier of match one char only or no match at all

you will need something like this to capture the whole string (Note the '+'):
$var =~ /n\\([^\\]+)\\/;

Replies are listed 'Best First'.
Re: Re: Regexpresions
by Sifmole (Chaplain) on Aug 20, 2001 at 16:48 UTC
    You probably were aware of this and just mistyped, but...

    The ? when used as it is in .? means zero or one, as stated by another poster above. .? does not mean one or more, that would be .+.

    A quick list:

    ? -- optional, zero or one + -- required and repeatable, one or more * -- optional and repeatable, zero or more
    All of these can be modified by placing a '?' after them, .+? in this usage however the '?' is affecting the greediness of the operator. Without the '?' it will attempt to match as many as possible (greedy), with the '?' it will attempt to match as few as required (non-greedy).

    There is a great book, Mastering Regular Expressions on this topic that is a very worthwhile read.

      D'Oh! Must be all these late nights... :-)

      Ah, thanks. The book i had suggested that .? would work in the way .+? does. Plus in the second regexpresion I ment to type:

      /\\([^\\]+)/

      The thing I was puzzeled about is perhaps better explained with the fact that this doesn't seem to work:

      /\\([^\\]+)\\/

      Maybe it was just that it was 4 in the morning at the time.