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,
In reply to Re^2: REGEX detailed character replace
by furry_marmot
in thread REGEX detailed character replace
by pjc955
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |