in reply to Match a comma between two words

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re^2: Match a comma between two words
by davido (Cardinal) on Jul 03, 2014 at 00:13 UTC

    1. My post did not suggest that you use the CSV module. It suggested that my solution correctly splits your sample input, but that things could get a lot more complicated quickly, and that is why people often recommend and use the CSV module.

    2. I tried to answer your questions, but the questions were incomplete sentence fragments that contained ambiguities and that didn't fully correlate with your sample input.. If you didn't have the initiative to investigate and understand the regex I used, how do you expect to solve the harder problems?

    3. My regex matches the comma between two words if it is surrounded by a quote on the left, and a quote on the right, which is exactly how your sample input is formatted. If you want to just match a comma between words, the regex would look like:

      /(?<=\p{Alpha}),(?=\p{Alpha})/

      But that doesn't take into consideration the quotes your sample input demonstrated. Now you've got two answers; one to the question you asked, and one adapted to the question you inferred by posting sample input slightly different from the exact question.

    4. My split example does that. But if you prefer to match multiple commas on a single line, the answer to that exact question is:

      while( $line =~ /,/g ) { print "Matched a comma.\n"; }

      That's probably not the question you really wanted to ask, but we got in trouble for misinterpreting ambiguous or incorrectly specified questions already. I must be sick for being willing to try again after being subjected to hostility in response to my voluntary effort, but I will do so. Perhaps you mean something like this:

      while( $line =~ m/(?:^|,)"([\p{Alpha}\s]+)"(?=,|$)/g ) { print "$1\n"; }

      Thiw works as long as none of the fields fall into the pitfalls I discussed in my original post. If they do, I recommend looking at the source code for Text::CSV to learn how that module handles the elevated rigors of balanced quotes and escaped delimiters.

      Here is the same regex in a slightly different scaffolding:

      print "$_\n" for $line =~ m/(?:^|,)"([\p{Alpha}\s]+)"(?=,|$)/g;

    Dave

Re^2: Match a comma between two words
by AnomalousMonk (Archbishop) on Jul 02, 2014 at 22:32 UTC

    Reading over the replies given so far, none seemed 'insulting' or in any way intentionally offensive; indeed, they seemed to be quite thoughtful and carefully composed.

    In any event, here are exact answers to your questions. I hope they will be helpful. You must of course define  $word as appropriate to your application. (Requires Perl version 5.10+ for the  \K regex construct.)

    c:\@Work\Perl>perl -wMstrict -le "use 5.010; ;; my $word = qr{ [^,]+ }xms; my $comma_between_2_words = qr{ $word \K , (?= $word) }xms; ;; for my $string (@ARGV) { print ''; printf 'question a: '; my $match = $string =~ $comma_between_2_words; print qq{'$string':}, $match ? '' : ' NO', ' match'; ;; printf 'question b: '; $match = $string =~ m{ , .* , }xms; print qq{'$string':}, $match ? '' : ' NO', ' match'; } " ",," ",x," "x,x" "," "x,y,z" question a: ',,': NO match question b: ',,': match question a: ',x,': NO match question b: ',x,': match question a: 'x,x': match question b: 'x,x': NO match question a: ',': NO match question b: ',': NO match question a: 'x,y,z': match question b: 'x,y,z': match

    Update: Changed example code to loop over  @ARGV for test strings.

Re^2: Match a comma between two words
by Tux (Canon) on Jul 03, 2014 at 10:48 UTC
    1. 1. Why not? OK, you want to learn, but the problem with CSV is that it looks easy at first but becomes exponentially more complex at every situation that is not simple, like embedded quotes, embedded separation characters (comma), embedded newlines, escaped comma's, (mixed) encodings, etc etc. I do not want to put you off (or insult you), but writing a real (decent) CSV parser is not something to take on as a learning process. OTOH, you could just fetch Text::CSV's code and look at how they do it and learn from that. Then you do not use it as a module, but all the questions you might come up with might have a working answer in the code.
    2. I didn't read any proof of that.
    3. Enjoy and behave


      Enjoy, Have FUN! H.Merijn
Re^2: Match a comma between two words
by BillKSmith (Monsignor) on Jul 03, 2014 at 11:46 UTC
    Please do not take offense. I believe that what the monks are trying to tell you is that this is probably not a good choice for a learning project. It is much more difficult than it appears. Use the module in your production work and choose another learning project.
    Bill
Re^2: Match a comma between two words
by Laurent_R (Canon) on Jul 03, 2014 at 23:15 UTC
    Hmm,

    Item 2: nobody said that you are stupid, nobody insulted you. Really. But you are sort of insulting people who tried to help you when you call ramble what they said. Same answer with respect to your last comment: Many thanks to those who will actually answer my questions without insulting me! Nobody insulted you. Or, if someone did, then I must be too stupid to see where, please show me where.

    At least 3 persons, including myself, tried to provide code snippets that tried to answer your questions without using a module before you posted your "pissed-off" post. But your questions were not very clear as to what you need exactly, and even your new post still does not really clarify what you need exactly: OK, you want to match comma(s), but what do you want to do with matched commas? Not clear to me. Usually, in a CSV, you are really interested with what if between the commas, the commas are just separators for the relevant information. I offered a solution based on this assumption. Is this not what you need? Well then clarify what you need and tell me why my proposed solution does not match your expectation. But don't say that I am ranting, don't say that I am insulting you, and don't say that monks here did not try to give you solutions.