Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Regex Remove String with Quotations

by nmdahl (Initiate)
on Jul 13, 2015 at 19:32 UTC ( [id://1134580]=perlquestion: print w/replies, xml ) Need Help??

nmdahl has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to format some French strings, and am having trouble with strings containing d". For example, if the input string is d"alice I need to change it to alice.

I found a statement that I thought would change find the d" and replace it with nothing, but it does not seem to work as I expected

 $myString =~ s/"d\""/" "/g;

Can anybody point me in the right direction?

Replies are listed 'Best First'.
Re: Regex Remove String with Quotations
by stevieb (Canon) on Jul 13, 2015 at 19:42 UTC

    Welcome to the Monastery!

    I think you're over complicating it with all the erroneous double-quotes. If it's just plain ascii, try this:

    s/d\"//g;

    If the d" is after a whitespace (ie. the beginning of a new word), you can do it like this to make the regex a bit more resilient:

    s/\bd\"//g;

    The \b is a "word boundary", and using it in the position I did means 'if a literal lowercase d is followed by a double-quote and is at the beginning of a word (ie. following any whitespace)', replace the d" with an empty string.

    Something that I just recently learned that was available and now use periodically besides YAPE::Regex::Explain is use re 'debug';. It might take a bit to get familiar with the output, but exceptionally valuable to learn if you want to understand why your regexes aren't (or are) matching when you think the opposite should be happening.

    -stevieb

Re: Regex Remove String with Quotations
by Laurent_R (Canon) on Jul 13, 2015 at 20:30 UTC
    Hm,

    'd"alice' hardly makes any sense in French (which happens to be my mother tongue), at least not without a much more elaborate context. You could expect "d'alice" to indicate something belonging or pertaining to a given person named Alice, but I can't see a sense to 'd"alice'.

Re: Regex Remove String with Quotations
by kcott (Archbishop) on Jul 14, 2015 at 05:36 UTC

    G'day nmdahl,

    Welcome to the Monastery.

    Here's a fairly straightforward way of doing that with a variety of test cases. Note this won't remove the 'd"' from the end of your first posted sentence (1st test case).

    #!/usr/bin/env perl use strict; use warnings; my $re = qr{\bd"(\w+)}; while (<DATA>) { s/$re/$1/g; print; } __DATA__ ... and am having trouble with strings containing d". ... if the input string is d"alice I need ... No d + " + alice here. Here's many: d"alice and d"alice and d"alice and ... d"alice at start of line and at end of line: d"alice "d"alice" 'd"alice'

    Output:

    ... and am having trouble with strings containing d". ... if the input string is alice I need ... No d + " + alice here. Here's many: alice and alice and alice and ... alice at start of line and at end of line: alice "alice" 'alice'

    If that doesn't do what you want, please post representative input and expected output. The guidelines in "How do I post a question effectively?" explain the best way to do that.

    — Ken

Re: Regex Remove String with Quotations
by stevieb (Canon) on Jul 13, 2015 at 20:47 UTC
    As Laurent_R said, we're pretty well just guessing at your input. Please post at least one real example string that you're working with.
Re: Regex Remove String with Quotations
by hurricup (Pilgrim) on Jul 13, 2015 at 19:37 UTC

    Encoding. What is the text file encoding?

Re: Regex Remove String with Quotations
by nmdahl (Initiate) on Jul 14, 2015 at 12:50 UTC

    Thank you for the help everyone! stevieb your solution was exactly what I was looking for. Just for everyone's reference, an example input would be appelle la maison d"Alice. The output in this case would be appelle maison Alice. I have had no problems removing la and similar words, but was having some difficulty with d" since it was part of a word I wanted to keep. But it is working now, thank you everyone.

      Once again, you would never see a sentence such as 'appelle la maison d"Alice' in French. Or only as an improbable error, because this is just plainly incorrect. In French, double quotes or guillemets are just the same thing as what they are in English and most European languages, i.e. a quoting punctuation character, not a part of the language itself.

      What you could see in a French sentence is "appelle la maison d'Alice", with a single quote (or apostrophe) between the "d" letter and the word "Alice".

      So your use case still does not make any sense to me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1134580]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found