Hi all, i've been trying to construct a regex to help with cleaning up names that can appear in some pretty nasty formats, it's raw data from government agencies.
Anyway, one thing i do is look for various 'care of' variants and standardize them to the % symbol. That is done through this regex:
$name =~ s/(^|\s)
C
(\/|\\|%)
O
(\s|$)
/ % /xgi;
(yes i am looking for the case of 'C%O' which i also see sometimes)
And then i want to replace forward of backslashes with &, as long as they do not have digits on both sides (which would be a fraction, which does sometimes appear in names i process). That is done through this regex:
$field =~ s/(?<!\d) # not preceeded by digits
(?:\\|\/) # back or forward slash
(?!\d) # not succeeded by digits
/ & /xg; # replace with '&' (globally)
The problem is that sometimes i want to perform only the second transformation without having done the first one, but i could not come up with any decent way to accomplish the second part with the exception of cases that are care-of's, as defined by the first part.
Any thoughts?
Update:
I got this working effectively, but then realized that my spec gets even worse, because if i see something like "D/B/A", i want to change that to "DBA" (Doing Business As), so this adds a completely new twist onto when and when not to replace the slashes. So i added this regex after the care of regex:
$field =~ s/(?:^|[^A-Z])
([A-Z])\/
([A-Z])\/
([A-Z])
(?:[^A-Z]|$)
/ $1$2$3 /xig;
Also, i think that using the separate regexes will work fine, i have worked around the problem of only wanting to perform the final stage without messing the earlier stages.
Thanks for all the suggestions!
I use the most powerful debugger available: print!
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.