Combining Regular Expressions

Posted on 2006-04-19
Last Modified: 2010-04-17
We're using a script to parse files, and we would like to combine some regular expressions:

sed s/" "/""/g | sed s/,/\",\"/g | sed s/"^"/\"/g | sed s/"$"/\"/g

Is there a way to combine this into one regular expression?

Question by:dandrzejewski
    LVL 9

    Accepted Solution

    This is to process a csv file.

    You are eliminating spaces, then replacing commas with quote-comma-quote then finally wrapping the line with quotes: In order to turn a comma separated set of unquoted strings into a quoted set of strings.

    I don't think you can do this in one regular expression because you are globally searching and replacing a sequence of patterns: This is at a higher level than the regular expression - more of a context free grammer type of thing - so you are into using perl perhaps and building a program around it.

    LVL 3

    Assisted Solution

    You can combine multiple sed rules into a single command like this:

    sed -e s/" "/""/g -e s/,/\",\"/g -e s/"^"/\"/g -e s/"$"/\"/g

    ..though I realise that's not exactly what you're asking, as it's still multiple regular expressions.

    Ignoring the s/" "/""/ rule, if there is a known number of columns in your original file, then you can do something like:


    With a regular expression, when you have \( <something> \), the actual text that matched the "something" can be inserted into the replacement string using \1, \2, \3.. for the first, second, third..., occurance of \(...\)

    Although this doesn't answer your question exactly, it might help.  I also agree with gabeso, that it probably isn't possible to do this as a single regular expression.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now