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

Request assistance with a 'simple' RegEx to search commas

Last post 02-04-2010, 6:11 PM by Aussie Susan. 3 replies.
Sort Posts: Previous Next
  •  02-01-2010, 12:58 PM 59158

    Request assistance with a 'simple' RegEx to search commas

    Hello,

    (Yes, I am very new with regular expressions and have done several searches for an answer, but have not found one. I do appreciate your time and help.)

    I am using TextPad to modify a file.

    The file contains several fields. When the field is populated, it is "string", "string", "string" (example).

    If the field is not populated, it is empty, as in , , , , -- no quote marks, only commas, no space in between commas.

    I need to use TextPad and run a Regular expression to:

    1. Add  quote marks in each of the -not- populated fields.

    2. Add quote marks in any field where there is a string, but no quotes, and keep the comma.

    3. Not change any field where there is already "string" -- keep the quotes and keep the string.

     So, I need to turn a line like this:

    10.14.14.24, "dd-excsql", , , , ,

    Into a line like this:

    "10.14.14.24", "dd-excsql", "", "", "", "",

    Thank you kindly.

  •  02-01-2010, 5:46 PM 59161 in reply to 59158

    Re: Request assistance with a 'simple' RegEx to search commas

    TextPad seems to use a father primitive regex engine (in comparison to other regex engines) in that it does not appear to allow for any form of lookaround which would make this fairly straight forward.

    However I have tried the following that seems to work. It requires 2 phases:

    Phase #1 - Convert all blank fields into null double-quoted strings

    Use the pattern:

    ,[:space:]?,

    with the replacement text

    ,"",

    You will need to start at the beginning of the text and use the "Replace All" option and then repeat from the beginning of the text again until you get a message that it "Can't find regular expression...".

    Phase #2 - put quote marks around unquoted fields

    Use the pattern:

    \(^\|,[:space:]?\)\([^"][^,]*\)

    with the replacement text

    \1"\2"

    Again, start at the beginning of the text and use the "Replace All" option. I believe that this should make all of the replacements in 1 pass but you might try several passes just to be sure - you will get the "Can't find regular expression..." message when it can't make any more substitutions.

    I tried this with the free evaluation version TextPad 5.3.1.

    By the way, I note that there is a space after ALL commas in your example string, even though you say that there is nothing between the commas. Both patterns allow for there to be an optional space after the comma.

    Susan

     

  •  02-04-2010, 11:13 AM 59287 in reply to 59161

    Re: Request assistance with a 'simple' RegEx to search commas

    Thanks Susan,

    I will start working with the information you sent.

    What is a better RegEx engine or product for Windows? I'd gladly take a recommendation, I've been trying several in working with this problem - TextPad, Notepad++, UltraEdit

    Robin

  •  02-04-2010, 6:11 PM 59313 in reply to 59287

    Re: Request assistance with a 'simple' RegEx to search commas

    I actually answered an almost identical question in another (unrelated) thread yesterday: http://regexadvice.com/forums/permalink/59190/59247/ShowThread.aspx#59247

    Susan

View as RSS news feed in XML