Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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( #75801=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on Apr 26, 2001 at 18:17 UTC
Q&A  > regular expressions


I need to use a regex to detect if a / character is at the beginning of a string.

Answer: How do I use a regex to identify a / character at the beginning of a string?
contributed by merlyn


Answer: How do I use a regex to identify a / character at the beginning of a string?
contributed by apotheon

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).

Answer: How do I use a regex to identify a / character at the beginning of a string?
contributed by szekszardi

if ($string =~ /^\//) { ... }

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (9)
    As of 2016-08-29 21:23 GMT
    Find Nodes?
      Voting Booth?
      The best thing I ever won in a lottery was:

      Results (409 votes). Check out past polls.