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



dpavlenkov
New User

Dec 5, 2003, 10:18 AM


Views: 27076
Alternative to alternation with |

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


Views: 27074
Re: [dpavlenkov] Alternative to alternation with |

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


Views: 27069
Re: [KevinR] Alternative to alternation with |

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


Views: 27068
Re: [dpavlenkov] Alternative to alternation with |

You could just escape the pipe Crazy



\|


KevinR
Veteran


Dec 5, 2003, 9:42 PM


Views: 27064
Re: [dpavlenkov] Alternative to alternation with |

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


Views: 27061
Re: [dpavlenkov] Alternative to alternation with |

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


Views: 26931
Re: [davorg] Alternative to alternation with |

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


Views: 26929
Re: [kencl] Alternative to alternation with |

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


Views: 26927
Re: [davorg] Alternative to alternation with |

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!