Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
hi,
I have a perl script to replace words or small phrases in a text file with a list that is saved to hash. I realized that when a phrase contains some symbols (i.e. #,.,(,), etc) the replacement is not performed.
Can you please have a look and let me know what's going wrong?
Here are the commands I use:
my $regex = join '|', map "\Q$_\E", sort {length $columnA <=> length $ +columnB} keys %dic; s/\b($regex)\b/$dic{$1}/g;
Thank you in advance for your time.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Symbols in regex
by Preceptor (Deacon) on Nov 17, 2015 at 11:42 UTC | |
Your problem is this:
That's not how sort works - it uses $a and $b to compare. Try instead:
Also - turn on use strict; use warnings; because this would have told you that:
The other gotcha is your use of \b - this is a word boundary, but it _doesn't_ match when you've got a symbol:
First doesn't match, because space-to-bracket isn't a word boundary: A word boundary (\b ) is a spot between two characters that has a \w on one side of it and a \W on the other side of it (in either order), - both space and bracket are \W. So you can't do it like that - either you've got to skip the word boundary element, decide how much you like whitespace, or use a lookaround. Or just don't worry about it and allow substring matching. Depends a bit on what data you've got to worry about something like:
Uses lookbehind/lookahead to detect spaces. (You might need to handle start/end of line too) - so a 'not word' negative assertion might be required instead:
This should match (word) as long as it isn't immediately preceeded by a alpha-numeric (so is ok with start of line, where (?<=\W) wouldn't be) and a trailing space or end-of-line. If your case is more complicated that that (e.g you need to _not_ match ((word) here) then this should at least give you a start point.
| [reply] [d/l] [select] |
by AnomalousMonk (Archbishop) on Nov 17, 2015 at 14:40 UTC | |
This approach has a subtle problem. The (?<!\w) and (?=\s|$) look-around assertions (see Look-Around Assertions) used as delimiters are ambiguous. One of the "words" (i.e., 'word word') in the dictionary contains a space, which matches both of the delimiters. The Perl regex alternation is "ordered", i.e., the first match in the alternation is the overall match, not the longest match. The sorting used to generate the regex in the code above is ascending; shorter strings with a common initial group of characters will sort before longer strings. This produces a "shortest first" match in the alternation because the delimiters are ambiguous. If the word 'word' is introduced into the translation dictionary along with the ambiguous 'word word' "word", a mistranslation occurs. This can be fixed by disambiguating the delimiters. Another fix is to build the alternation so that a "longest first" match is performed. In the example below, $rx_A is built from an ascending sort and produces a mistranslation. $rx_D is built from a descending sort that produces a longest-first match and translates properly. (If such a "longest" alternation is used, delimiters can often be dispensed with entirely. In general, I prefer to build "longest" alternations from lists into which ambiguous strings may creep.)
See sort for other ways to produce ascending versus descending sorting. Update: Here's an example where the (reversed) default lexical sort alone is sufficient to produce proper, longest-first translation entirely without delimiters:
Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
|
Re: Symbols in regex
by Eily (Monsignor) on Nov 17, 2015 at 10:57 UTC | |
sort {length $columnA <=> length $columnB} keys %dic; where do $columnA and $columnB come from? If you print $regex; do you get the expected result (an actually sorted list of alternatives?). If you don't give us a fully working exemple we can only guess blindly. See How do I post a question effectively? And to give us your input data, you can do something like:
| [reply] [d/l] [select] |
|
Re: Symbols in regex
by AnomalousMonk (Archbishop) on Nov 17, 2015 at 13:12 UTC | |
Because you posted anonymously, you can't change a post; someone has to follow along behind you and clean up the mess. Please register as a user on PerlMonks. Posting as a registered, logged-in user allows you to edit a post. Please see How do I change/delete my post? for info on this, including proper etiquet for changing posts. Give a man a fish: <%-{-{-{-< | [reply] [d/l] |
|
Re: Symbols in regex
by Anonymous Monk on Nov 17, 2015 at 10:50 UTC | |
| [reply] |
|
Re: Symbols in regex
by Anonymous Monk on Nov 17, 2015 at 11:35 UTC | |
Hi, Thanks for your reply! I am sorry that I didn't tell you that.
The DIC file is a tab-delimeted file. For instance,
The input text looks like: This is a (word)and should change to: This is a (parola) | [reply] [d/l] [select] |
|
Re: Symbols in regex
by Anonymous Monk on Nov 17, 2015 at 11:20 UTC | |
Hi, Thanks for your reply! I am sorry that I didn't tell you that.
The DIC file is a tab-delimeted file. For instance,
| [reply] [d/l] [select] |