$foo = qq{^snafu^|^foobar^\n};
$foo =~ m/\A(\W) # \A instead of ^ and match first non-word
.+? # +? Minimal match everything that isn't in \1
\1(\W) # Match non-word following the 2nd \1
/xms;
$TEXT_QUAL = $1;
$FIELD_SEP = $2;
Having now found the proper way to attempt to acquire delimiters, the following questions how to utilize these new found delimiters.
Instead of creating one large regex, I'd perfer to store them in scalars, which is the core of this particular problem.
$foo =~ /\G$TEXT_QUAL(.*?)$TEXT_QUAL[$FIELD_SEP\n]/xmsgc;
Fails to work since the qualifiers are metacharacters used in regular expressions.
$foo =~ /\G\$TEXT_QUAL(.*?)\$TEXT_QUAL[\$FIELD_SEP\n]/xmsgc;
Fails to work as \$ is a literal $ followed by the name.
$foo =~ /\G\\$TEXT_QUAL(.*?)\\$TEXT_QUAL[\\$FIELD_SEP\n]/xmsgc;
Also Fails to work as \\ is is a literal \
The only way I've found is
$LIT_TEXT_QUAL = qq{\\$TEXT_QUAL};
$LIT_FIELD_SEP = qq{\\$FIELD_SEP};
$foo =~ /\G$LIT_TEXT_QUAL(.*?)$LIT_TEXT_QUAL[$LIT_FIELD_SEP\n]/xmsgc;
I do have reasons for using all those flags as this thread continues, however with the intent to get discrete answers to smaller problems I'm hoping to reduce the amount of new information my brain will have to process.
Basically this post is looking for a way to use any variable in a regex that may or may not contain metachacters.
Edit:: OK yeah missed the boat on this one, answer is just quotemeta function, from CB thanx guys!
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.