Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How do I use a regex to identify a / character at the beginning of a string?

by Anonymous Monk
on Apr 26, 2001 at 18:17 UTC ( [id://75801]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to use a regex to detect if a / character is at the beginning of a string.
  • Comment on How do I use a regex to identify a / character at the beginning of a string?

Replies are listed 'Best First'.
Re: How do I use a regex to identify a / character at the beginning of a string?
by merlyn (Sage) on Apr 26, 2001 at 18:18 UTC
    m{^/}
Re: How do I use a regex to identify a / character at the beginning of a string?
by apotheon (Deacon) on Oct 19, 2004 at 23:23 UTC
    What many regex users don't realize is that the slashes aren't the designators for regex matching. The actual designator is the letter m, and the slashes are only delimiters. Because the most basic use of regular expressions is to match strings, however, the process of matching has been abbreviated — aliased, if you will — so that omitting the m is possible as long as the default delimiters (slashes) are used. This causes problems if the default delimiters and the string for which you're searching are one and the same, however.

    Other characters can be substituted for the slash in such circumstances, or the slashes within the matching pattern can be escaped.

    The escape character option is the most obvious to those who think of slashes as the actual designators of a matching regex. For such an approach, one might use either of the following code examples to search for a slash using a matching regular expression.
    example 1: /^\//
    example 2: m/^\//

    To substitute other characters, one must be certain to use the letter m to designate the matching regular expression. I provide a couple examples of substitute delimiters.
    example 1: m@^/@
    example 2: m#^/#

    Using alternative delimiters is something that happens "on the fly". That is to say, you don't have to predefine the use of alternate delimiters, and the next time you use a matching regexp in the same script you don't have to keep using the same alternate delimiters. Thus, you can use /^\// in a script, follow it later with m#^/# in the same script, and still later use m/^\// (all without running afoul of any restrictions in how Perl regexp syntax is used).
Re: How do I use a regex to identify a / character at the beginning of a string?
by szekszardi (Initiate) on May 10, 2001 at 16:11 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found