How do I match a | in a regular expression?

J

Joe Saldana

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top