in reply to REGEX detailed character replace
Here's a slightly different approach to the problem. Note that the treatment of the space is different in the example and the specification.> This is what I have: > |xx-xxx-xxxxx-xxx x/xx|xx-xxxx-xxx-xx-xxxx-xx-xx| > This is what I want: > xx-xxx-xxxxx-xxx,x/xx,xx,xxxx,xxx,xx,xxxx,xx,xx,
Since all the exceptions are at the beginning of the string, we can handle the removal of the leading pipe and then sequester the first part of the string. The remainder can be handled with the "tr" command.
Advantages of this approach:
#!/usr/bin/perl use strict; my $string = '|xx-xxx-xxxxx-xxx x/xx|xx-xxxx-xxx-xx-xxxx-xx-xx|'; print "$string\n"; # remove pipe, protect everything before the space $string =~ s{^[|](\S+)}{}; my $result = $1; # translate pipes, spaces, and hyphens to commas $string =~ tr[| -][,]; $result .= $string; print "$result\n";
|
|---|