in reply to REGEX detailed character replace

Learn some regex-fu. :)

Alright, well, let's take what you have:

|xx-xxx-xxxxx-xxx x/xx|xx-xxxx-xxx-xx-xxxx-xx-xx|

Now re-write that in a way that we preserve what you want to keep:

|(xx)-(xxx)-(xxxxx)-(xxx) (x/xx)|(xx)-(xxxx)-(xxx)-(xx)-(xxxx)-(xx)-(x +x)|

Make that a bit more flexible:

|(\w{2})-(\w{3})-(\w{5})-(\w{3}) (\w/\w{2})|(\w{2})-(\w{4})-(\w{3})-(\ +w{2})-(\w{4})-(\w{2})-(\w{2})|
Now you'll match all of the interesting bits, so let's set up the inline replace:
$mess =~ s/|(\w{2})-(\w{3})-(\w{5})-(\w{3}) (\w\/\w{2})|(\w{2})-(\w{4} +)-(\w{3})-(\w{2})-(\w{4})-(\w{2})-(\w{2})|/\1,\2,\3,\4\5,\6,\7,\8,\9, +\10,\11,\12/;

Replies are listed 'Best First'.
Re^2: REGEX detailed character replace
by pjc955 (Initiate) on Nov 12, 2008 at 16:42 UTC
    Thank you everyone for your help! I had looked all over and couldn't find a solution and you all have provided me with multiple ways to go about what I need which is fantastic.
    I really appreciate the input.
Re^2: REGEX detailed character replace
by furry_marmot (Pilgrim) on Nov 13, 2008 at 20:34 UTC

    What these solutions seem to be missing is the OP wants to achieve a transformation, rather than capturing the individual 'x' groupings. More specifically, part of the string will be preserved in a known manner, while the rest is transformed.

    In addition, I assume the 'x' characters are stand-ins for real alphanumeric data. So, now I think we can generalize the regex a lot via these rules:

    - Skip a pipe at the beginning of the line.
    - Grab a) alphanumerics and dashes up to third occurance of dash, b) the rest.
    - In "the rest", replace pipes and dashes with commas.
    - OP says "remove spaces", but his example shows the space being replaced by a comma, so do that.

    $s='|xx-xxx-xxxxx-xxx x/xx|xx-xxxx-xxx-xx-xxxx-xx-xx|'; # in one line, if not one command ($a, $b)=$s=~/^\|?(\w+-\w+-\w+-)(.+)/; $b=~s/[-| ]/,/g; $s=$a . $b; print $s # xx-xxx-xxxxx-xxx,x/xx,xx,xxxx,xxx,xx,xxxx,xx,xx,