1

I have a Line that is pipe delimited:

John |DEMME|"9 Snowy "" Court"|WERRIBEE|""VIC""

I split my line to each fields

@fields = split (/\|/, $_);

what I want is to remove the double quotes in the beginning/end of each fields but it should retain the double quotes that are in between.

expected output

John |DEMME|9 Snowy "" Court|WERRIBEE|VIC

I also tried this

s/^\"|"$//g;

but what it does is it reads by line not but fields, so it will only remove the double qoutes which are at the beginnig and end of the line.

another scenario:

John |DEMME| "Shop 6A ""Atlantic on Coolum""|WERRIBEE|VIC

output should be

John |DEMME| Shop 6A "Pacific on Coolum"|WERRIBEE|VIC

I hope you guys can help me with this.

thank you very much

5
  • Shouldn't that be John |DEMME|9 Snowy " Court|WERRIBEE|VIC?
    – ikegami
    Apr 4, 2013 at 2:14
  • @ikegami nope it is okay thanks:)
    – Soncire
    Apr 4, 2013 at 2:59
  • @ikegami but what if I want the output like that, what am I going to change in this regex? s/(?:^|(?<=\|))"|"(?=$|\|)//g;
    – Soncire
    Apr 4, 2013 at 3:04
  • I'd go looking at Text::CSV_XS and use a pipe as your separator. Apr 4, 2013 at 4:20
  • If "" is an escaped ", I'd use Text::CSV_XS to parse.
    – ikegami
    Apr 4, 2013 at 8:09

3 Answers 3

2

If it's reading by lines, then you could write:

s/(?:^|(?<=\|))"|"(?=$|\|)//g;

to remove " at the start of a line or after a |, or at the end of a line or before a |.

(The (?<=...) notation creates a zero-width positive "lookbehind" assertion, which in this case checks to see if there's a | preceding; the (?=...) notation creates a zero-width positive "lookahead" assertion, which in this case checks to see if there's a | or end-of-file following.)

6
  • thanks that was great! It worked! but if it is not too much trouble to ask, can you please at least help me understand your code, I really want to learn perl regex. thanks
    – Soncire
    Apr 4, 2013 at 1:56
  • @Soncire: Sure. Which part are you having trouble with?
    – ruakh
    Apr 4, 2013 at 2:32
  • (?:^|(?<=\|)) ->this part "|" -> this part (?=$|\|) -> this part :)
    – Soncire
    Apr 4, 2013 at 2:56
  • @Soncire: "|" actually isn't a part; | indicates alternation, and here the two alternands are (?:^|(?<=\|))" (which ends with ") and "(?=$|\|) (which starts with "). As for (?:^|(?<=\|)) . . . (?:...) is just a grouping construct; for example, (?:a|b)+ means "a string of one or more a's and b's" (whereas a|b+ means "either a single a, or else a string of one or more b's). ^ means "start of string", and | I've already explained. (?<=...), as I mentioned in my answer, is a lookbehind assertion, so (?<=\|) means "a position that is preceded by a pipe. So [continued]
    – ruakh
    Apr 4, 2013 at 16:35
  • [continued] (?:^|(?<=\|)) means "either start-of-string, or right after a pipe". Similarly, (?=...), as I mentioned in my answer, is a lookahead assertion, so (?=$|\|) means "either end-of-string, or right before a pipe".
    – ruakh
    Apr 4, 2013 at 16:36
1

try this one dude

my $_='John |DEMME|"9 Snowy "" Court"|WERRIBEE|""VIC""';

my @fields = split (/\|/, $_);

foreach my $item(@fields){
 $item=~s/^\"+//g;
 $item=~s/\"+$//g;
print "$item";

}
0

The quotes could also be removed from each field using a map expression:

@fields = map { s/^"(.*)"$/$1/; $_  } split (/\|/, $_);

split breaks the line apart into a list, while the map applies the substitution to each member of the list. To join them back up:

print join('|', @fields), "\n";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.