CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Alternative to alternation with |

 



dpavlenkov
New User

Dec 5, 2003, 10:18 AM

Post #1 of 9 (12602 views)
Alternative to alternation with | Can't Post

Is there a way to create alternation without using | character? I am using a silly program that parses config file by splitting on |, and that destroys any regex with alternation.

I would modify the program to use a different split character, but it is spread all over the code and perhaps that's too much work.

Any lazy people with suggestions?Crazy


KevinR
Veteran


Dec 5, 2003, 10:34 AM

Post #2 of 9 (12600 views)
Re: [dpavlenkov] Alternative to alternation with | [In reply to] Can't Post

have you tried using quotemeta?

$yourstring = quotemeta($yourstring);

will escape non word characters in the string. You can try it and see if it will work for your purpose.

But why is spliting on the | messing you up? The character used for the split is removed from the string.
-------------------------------------------------


dpavlenkov
New User

Dec 5, 2003, 11:24 AM

Post #3 of 9 (12595 views)
Re: [KevinR] Alternative to alternation with | [In reply to] Can't Post

quotemeta would probably work, but config file is not in perl, it's simply a hash with | separators. like this:

key1|value1

key2|value2

so you see if a value (for instance a regex) contains a | character, it will be cut off at that character by the parser.

but there, I answered my own question, if I put a limit of 2 in split, that won't be a problem anymore, because it will only split once. Sly


Recall
Novice

Dec 5, 2003, 1:02 PM

Post #4 of 9 (12594 views)
Re: [dpavlenkov] Alternative to alternation with | [In reply to] Can't Post

You could just escape the pipe Crazy



\|


KevinR
Veteran


Dec 5, 2003, 9:42 PM

Post #5 of 9 (12590 views)
Re: [dpavlenkov] Alternative to alternation with | [In reply to] Can't Post

you mean the value can contain the file delimiter character? You should code your script so that the file delimiter is not allowed or is escaped or converted to ascii code.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Dec 7, 2003, 10:52 AM

Post #6 of 9 (12587 views)
Re: [dpavlenkov] Alternative to alternation with | [In reply to] Can't Post

Giving split a third argument of 2 would be the easiest fix.

But if you can't do that, how about using a different character to represent the pipe and then converting it once it's been read in from the config file.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


kencl
User

Jul 12, 2004, 6:15 AM

Post #7 of 9 (12457 views)
Re: [davorg] Alternative to alternation with | [In reply to] Can't Post

Why do you say he can't do that? That's exactly what the limit parameter is for.

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


davorg
Thaumaturge / Moderator

Jul 12, 2004, 6:29 AM

Post #8 of 9 (12455 views)
Re: [kencl] Alternative to alternation with | [In reply to] Can't Post

I didn't mean to imply that he shouldn't do that. That is, of course, the best solution.

But in some cases, that solution won't be possible. I'm thinking of situations where (for example) the key value contains the separator.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


kencl
User

Jul 12, 2004, 6:31 AM

Post #9 of 9 (12453 views)
Re: [davorg] Alternative to alternation with | [In reply to] Can't Post

ah, makes sense. Amazing how small the word "if" can be...

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives