How do I match a | in a regular expression?

Discussion in 'Perl Misc' started by Joe Saldana, Dec 18, 2006.

  1. Joe Saldana

    Joe Saldana Guest

    I'm trying to write a script that will accept user input for a number of
    variables. One of the variables will be a pipe for certain files. I can
    read and store the | character in a variable. Later on, when I want to use
    that variable in a match, I can't figure out how to tell perl that I am
    trying to match a literal |. If I use an escaped | ( \| ), the match works
    fine. I don't want alternation; I'd like to match a certain string followed
    by any amount of whitespace followed by a delimiter (which will be "|"
    sometimes) followed by more whitespace (any/none) followed by a second
    variable. Right now, that looks like this:

    if ($record =~ /$key\s*?\|\s*?$value/)

    That, of course, is with the literal pipe. Assuming that I had the |
    character in a variable called $Delmiter, is there a way for me to replace
    the \| with $Delimiter in the match? That way the | wouldn't be hard-coded,
    and I'd be able to use other delmiters specified by the user.

    Thanks,
    Joe
     
    Joe Saldana, Dec 18, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. championsleeper
    Replies:
    6
    Views:
    1,051
    championsleeper
    Apr 6, 2004
  2. Liang
    Replies:
    2
    Views:
    1,767
  3. VSK
    Replies:
    2
    Views:
    2,379
  4. Replies:
    4
    Views:
    749
  5. Replies:
    0
    Views:
    377
Loading...

Share This Page