Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

matching all the commas that are not inside quotes

Last post 11-08-2006, 10:31 PM by Sergei Z. 4 replies.
Sort Posts: Previous Next
  •  11-07-2006, 2:39 PM 23957

    matching all the commas that are not inside quotes

    I've been banging my head against the wall with this one, and regex is not my strong suit anyhow. I have no control over the built string to start (the CFPop tag in ColdFusion generates it). The trouble comes in when there are entries with commas in them as well (eg "grey, morgan" greykher@nospam.com).

    What I need is to use REReplace() or similar to change a string that looks like:

    "grey, morgan" <greykher@nospam.com>,spamfree@nospam.com

    to:

    "grey, morgan" <greykher@nospam.com>;spamfree@nospam.com

    Change the comma between addresses to semicolon, but leave the comma in the display names alone. I found this ""[^""]*""|[^,]+ at (http://regexadvice.com/blogs/wayneking/archive/2004/01/12/271.aspx) that says it does this, but testing has it matching the comma inside the quotes as well. To me, this seems like it should be as simple as matching all the commas that are not inside quotes, but I can't get it to work so I'm open to any suggestions. If I can find a RegEx that will match right, I can get the REReplace() to work, no problem.

    thanks

     -morgan

  •  11-07-2006, 3:08 PM 23958 in reply to 23957

    Re: matching all the commas that are not inside quotes

    ..asuming u r validating hte strings on a separate line [or as a separate string]: 

     logic: ***find comma followed by a sequence of chars, which DOES NOT include char \x22  [i.e. double quote] and ends with the end of Input [end of line]; replace it with a semicolon ***

    implement: 

    match with

    (,)(?=[^\x22]+$)

    replace with

    ;

    Note: this assumes that yr regex engine supports a variable-length look-arounds. Check with CF docs, i have doubts it is the case.

    If not, then u can try [this only will work with 1 occurrence of the targeted comma]:

    match with

    (,)([^\x22]+$)

    replace with

    ;$2

    run vs the input:

    "grey, morgan" <greykher@nospam.com>,spamfree@nospam.com

    it'll result in:

    "grey, morgan" <greykher@nospam.com>;spamfree@nospam.com

     

     

  •  11-08-2006, 1:22 PM 23992 in reply to 23958

    Re: matching all the commas that are not inside quotes

    Assumming the strings format is the same at all times

    Try this one. 

    ^(\x22[^\x22]+\x22.*?)\,(.*?)$

    replace with

    $1;$2

     


    "When the only tool you have is an hammer, everything tends to look like nails" A. Maslow
  •  11-08-2006, 7:00 PM 24001 in reply to 23957

    Re: matching all the commas that are not inside quotes

    One way:
    Pattern:     ((?:"[^"]*"|[^",])*),
    Replace:    $1;

    Another way(if your tool supports evaluating code on replacement part):
    Pattern:     ("[^"]*")|,
    Replace:    defined($1) ? $1 : ";"              <= a code

    In Perl, the second way can be written as: 

       s/("[^"]*")|,/ $1 or ";"/eg

    Regards,
    Xicheng


    perl -le 'print"So~*kde~box*DS*Zoxf*fe|er"^$\x23'
  •  11-08-2006, 10:31 PM 24002 in reply to 24001

    Re: matching all the commas that are not inside quotes

    thanx for the science, Xicheng, it's brilliant - i am seeing the approach of yours n-th time, and now i'm finally getting it.

    Stop by more often.

    how is your defense coming along?

    Best,

    Sergei

View as RSS news feed in XML