Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Regexpresions

by ozone (Friar)
on Aug 20, 2001 at 16:32 UTC ( #106200=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://106200]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2022-05-18 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?