need regular expression to replace part of result based on a search pattern

Discussion in 'Java' started by Jimmy, Jul 11, 2012.

  1. Jimmy

    Jimmy Guest

    Have been having hard time to come up with the regular expression to replace the following..

    1,"blaha",NULL,'N','Y','N','N',NULL
    2,"blahb",NULL,'Y','Y','Y','N',NULL
    3,"blahc",NULL,'N','N','N','N',NULL
    ^

    With all contents in column #6 (first column is column #1) to 'Y'?

    i.e. result

    1,"blaha",NULL,'N','Y','Y','N',NULL
    2,"blahb",NULL,'Y','Y','Y','N',NULL
    3,"blahc",NULL,'N','N','Y','N',NULL
    ^

    Thanks,
    Jimmy
     
    Jimmy, Jul 11, 2012
    #1
    1. Advertising

  2. Jimmy

    Roedy Green Guest

    On Wed, 11 Jul 2012 10:35:05 -0700 (PDT), Jimmy
    <> wrote, quoted or indirectly quoted someone
    who said :

    >With all contents in column #6 (first column is column #1) to 'Y'?
    >
    >i.e. result


    you result looks identical to what you started with.

    Perhaps your problem is which characters to quote.

    See http://mindprod.com/applet/quoter.html

    "3,\"blahc\",NULL,'N','N','N','N',NULL"

    you can the insert variable bits

    "[0-9],\"blah[a-z]\",NULL,'N','N','N','N',NULL"
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Mathematicians and computer scientists are far more interested
    in impressing you than informing you. If this were not
    so, the tutorials on building a robots.txt file, for example,
    would consist primarily of an annotated example. What you get
    instead are nothing but inscrutable adstract fragments in some
    obscure dialect of BNF.
     
    Roedy Green, Jul 11, 2012
    #2
    1. Advertising

  3. Re: need regular expression to replace part of result based on asearch pattern

    On 7/11/2012 10:35 AM, Jimmy wrote:
    > Have been having hard time to come up with the regular expression to replace the following..
    >
    > 1,"blaha",NULL,'N','Y','N','N',NULL
    > 2,"blahb",NULL,'Y','Y','Y','N',NULL
    > 3,"blahc",NULL,'N','N','N','N',NULL
    > ^
    >
    > With all contents in column #6 (first column is column #1) to 'Y'?
    >
    > i.e. result
    >
    > 1,"blaha",NULL,'N','Y','Y','N',NULL
    > 2,"blahb",NULL,'Y','Y','Y','N',NULL
    > 3,"blahc",NULL,'N','N','Y','N',NULL
    > ^
    >
    > Thanks,
    > Jimmy
    >


    public class test {
    public static void main(String[] args) {
    String str = "3,\"blaha\",NULL,'N','N','N','N',NULL";

    System.out.println(str);

    String[] arr = str.split(",");
    if (arr[5].equals("'N'"))
    arr[5] = "'Y'";

    StringBuilder sb = new StringBuilder();
    for (int i=0; i<arr.length; i++) {
    sb.append(arr);
    if (i != arr.length - 1)
    sb.append(",");
    }
    System.out.println(sb);
    }
    }
     
    Knute Johnson, Jul 11, 2012
    #3
  4. Re: need regular expression to replace part of result based on asearch pattern

    On 12/07/2012 00:40, Knute Johnson allegedly wrote:
    > On 7/11/2012 10:35 AM, Jimmy wrote:
    >> Have been having hard time to come up with the regular expression to
    >> replace the following..
    >>
    >> 1,"blaha",NULL,'N','Y','N','N',NULL
    >> 2,"blahb",NULL,'Y','Y','Y','N',NULL
    >> 3,"blahc",NULL,'N','N','N','N',NULL
    >> ^
    >>
    >> With all contents in column #6 (first column is column #1) to 'Y'?
    >>
    >> i.e. result
    >>
    >> 1,"blaha",NULL,'N','Y','Y','N',NULL
    >> 2,"blahb",NULL,'Y','Y','Y','N',NULL
    >> 3,"blahc",NULL,'N','N','Y','N',NULL
    >> ^
    >>
    >> Thanks,
    >> Jimmy
    >>

    >
    > public class test {
    > public static void main(String[] args) {
    > String str = "3,\"blaha\",NULL,'N','N','N','N',NULL";
    >
    > System.out.println(str);
    >
    > String[] arr = str.split(",");
    > if (arr[5].equals("'N'"))
    > arr[5] = "'Y'";
    >
    > StringBuilder sb = new StringBuilder();
    > for (int i=0; i<arr.length; i++) {
    > sb.append(arr);
    > if (i != arr.length - 1)
    > sb.append(",");
    > }
    > System.out.println(sb);
    > }
    > }
    >


    Not handling the case where there is a separator (comma) within a
    (quoted) field.

    Regex could only be a solution if such a case were excluded.

    --
    DF.
     
    Daniele Futtorovic, Jul 11, 2012
    #4
  5. Jimmy

    Roedy Green Guest

    On Wed, 11 Jul 2012 10:35:05 -0700 (PDT), Jimmy
    <> wrote, quoted or indirectly quoted someone
    who said :

    >1,"blaha",NULL,'N','Y','N','N',NULL
    >2,"blahb",NULL,'Y','Y','Y','N',NULL
    >3,"blahc",NULL,'N','N','N','N',NULL

    this format is similar to a CSV file. Just get consistent on your
    quote char.

    You can then read it with CSVReader. See
    http://mindprod.com/application/csv.manual.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Mathematicians and computer scientists are far more interested
    in impressing you than informing you. If this were not
    so, the tutorials on building a robots.txt file, for example,
    would consist primarily of an annotated example. What you get
    instead are nothing but inscrutable adstract fragments in some
    obscure dialect of BNF.
     
    Roedy Green, Jul 12, 2012
    #5
  6. Jimmy

    Jimmy Guest

    Re: need regular expression to replace part of result based on asearch pattern

    Thank you so much for everyone's response.
    These CSV are inside a script and I've clean up the quotes.

    Want 1 line of regexp to change from column #6 (from top to bottom 'N','Y','N','Y','N','Y')

    'blaha',NULL,'N','Y','N','N',NULL
    'blahb',NULL,'Y','Y','Y','N',NULL
    'blahc',NULL,'N','N','N','N',NULL
    'blahd',NULL,'N','N','Y','N',NULL
    'blahe',NULL,'N','N','N','N',NULL

    To following result in column #6 (from top to bottom 'Y','Y','Y','Y','Y','Y')

    'blaha',NULL,'N','Y','Y','N',NULL
    'blahb',NULL,'Y','Y','Y','N',NULL
    'blahc',NULL,'N','N','Y','N',NULL
    'blahd',NULL,'N','N','Y','N',NULL
    'blahe',NULL,'N','N','Y','N',NULL

    Can someone please help?

    Thanks again,
    Jimmy
     
    Jimmy, Jul 12, 2012
    #6
  7. Jimmy

    Eric Sosman Guest

    Re: need regular expression to replace part of result based on asearch pattern

    On 7/12/2012 10:28 AM, Jimmy wrote:
    > Thank you so much for everyone's response.
    > These CSV are inside a script and I've clean up the quotes.
    >
    > Want 1 line of regexp to change from column #6 (from top to bottom 'N','Y','N','Y','N','Y')
    >
    > 'blaha',NULL,'N','Y','N','N',NULL
    > 'blahb',NULL,'Y','Y','Y','N',NULL
    > 'blahc',NULL,'N','N','N','N',NULL
    > 'blahd',NULL,'N','N','Y','N',NULL
    > 'blahe',NULL,'N','N','N','N',NULL
    >
    > To following result in column #6 (from top to bottom 'Y','Y','Y','Y','Y','Y')
    >
    > 'blaha',NULL,'N','Y','Y','N',NULL
    > 'blahb',NULL,'Y','Y','Y','N',NULL
    > 'blahc',NULL,'N','N','Y','N',NULL
    > 'blahd',NULL,'N','N','Y','N',NULL
    > 'blahe',NULL,'N','N','Y','N',NULL
    >
    > Can someone please help?


    If you are certain that commas appear *only* as field separators
    and never inside quoted material, you can use something like (untested)

    "(.*?,){5}.*?,(.*)"

    After matching you'd then paste group(1) + "'Y'," + group(2).

    If quoted material can contain commas that don't count as field
    separators, I don't think regular expressions can do the job at all.
    You'll need to parse the strings in a context-sensitive way.

    "Some people, when confronted with a problem, think `I know,
    I'll use regular expressions.' Now they have two problems."
    -- Jamie Zawinski

    --
    Eric Sosman
    d
     
    Eric Sosman, Jul 12, 2012
    #7
  8. Re: need regular expression to replace part of result based on asearch pattern

    On 7/12/2012 11:24 AM, Eric Sosman wrote:
    > If quoted material can contain commas that don't count as field
    > separators, I don't think regular expressions can do the job at all.
    > You'll need to parse the strings in a context-sensitive way.


    ((?:[^,]*|'[^']*'),){5}

    If you can have escapes, your string quote portion becomes:
    '([^\\']|\\.)' (not including escape characters you need to represent
    this is a Java string literal). It's doable, but building this sort of
    stuff gets very ugly very quickly.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Jul 12, 2012
    #8
  9. On Thu, 12 Jul 2012 09:42:55 -0500, Leif Roar Moldskred
    <> wrote:

    >Jimmy <> wrote:
    >> Have been having hard time to come up with the regular expression to replace the following..

    >
    >Then don't use a regular expression.
    >
    >There's a quote by Brian Kernighan that “Debugging is twice as hard as
    >writing the code in the first place. Therefore, if you write the code
    >as cleverly as possible, you are, by definition, not smart enough to
    >debug it.”


    A bit of exaggeration, but not much.

    >In other words, if it's not quickly obvious how to write a regular
    >expression that does what you want, use something else. Your
    >maintenance programmers (often your six-month older self) will thank
    >you.


    If you really, really, really have to do it, know that you will
    end up with Write-Only code so isolate as much as you can. Document
    it, and hope that you never have to deal with it again. If you do,
    you will appreciate the documentation.

    >From the description you've given of the problem, I would think that
    >your best bet is to parse your input as a CSV, do the required
    >changes, and write it back into a CSV. Easier to write and easier to
    >read than a dose of line noise.


    And easier to modify. What if you later have to also modify
    column 4 to always have 'N'? If your first reaction is negative, then
    your code is too complex.

    I write a lot of my code without worrying much about how fast or
    elegant it is. Usually, it is quite fast enough, and it is
    maintainable.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Jul 12, 2012
    #9
  10. Leif Roar Moldskred <> wrote:

    (snip)
    > There's a quote by Brian Kernighan that ???Debugging is twice as hard as
    > writing the code in the first place. Therefore, if you write the code
    > as cleverly as possible, you are, by definition, not smart enough to
    > debug it.???


    I believe one of Brooks' laws goes something like:

    "Writing the program takes the first 90% of the time,
    debugging the second 90%."

    So, according to that it takes the same amount of time, but nine
    times as long as you thought it would. (Based on his experience
    with OS/360.)

    -- glen
     
    glen herrmannsfeldt, Jul 12, 2012
    #10
  11. Jimmy

    Arne Vajhøj Guest

    Re: need regular expression to replace part of result based on asearch pattern

    On 7/11/2012 1:35 PM, Jimmy wrote:
    > Have been having hard time to come up with the regular expression to replace the following..
    >
    > 1,"blaha",NULL,'N','Y','N','N',NULL
    > 2,"blahb",NULL,'Y','Y','Y','N',NULL
    > 3,"blahc",NULL,'N','N','N','N',NULL
    > ^
    >
    > With all contents in column #6 (first column is column #1) to 'Y'?
    >
    > i.e. result
    >
    > 1,"blaha",NULL,'N','Y','Y','N',NULL
    > 2,"blahb",NULL,'Y','Y','Y','N',NULL
    > 3,"blahc",NULL,'N','N','Y','N',NULL


    Code for inspiration:

    import java.util.regex.Pattern;

    public class July {
    private static final String fmt = "(%s(,%s){4},)(%s)((,%s){2})";
    private static final String col1 = "[^,]*";
    private static final Pattern pat1 = Pattern.compile(String.format(fmt,
    col1, col1, col1, col1));
    public static String reReplace1(String s) {
    return pat1.matcher(s).replaceAll("$1'Y'$4");
    }
    private static final String col2 = "(\\d+|(\"[^\"]*\")|('[^']*')|(NULL))";
    private static final Pattern pat2 = Pattern.compile(String.format(fmt,
    col2, col2, col2, col2));
    public static String reReplace2(String s) {
    return pat2.matcher(s).replaceAll("$1'Y'$16");
    }
    public static void test(String s) {
    System.out.println(s);
    System.out.println(reReplace1(s));
    System.out.println(reReplace2(s));
    }
    public static void main(String[] args) {
    test("1,\"blaha\",NULL,'N','Y','N','N',NULL");
    test("2,\"blahb\",NULL,'Y','Y','Y','N',NULL");
    test("3,\"blahc\",NULL,'N','N','N','N',NULL");
    test("4,\"blahd,nasty\",NULL,'N','N','N','N',NULL");
    }
    }

    Arne
     
    Arne Vajhøj, Jul 24, 2012
    #11
  12. Re: need regular expression to replace part of result based on asearch pattern

    On 7/12/2012 10:42 AM, Leif Roar Moldskred wrote:
    > Jimmy <> wrote:
    >> Have been having hard time to come up with the regular expression to replace the following..

    >
    > Then don't use a regular expression.
    >
    > There's a quote by Brian Kernighan that “Debugging is twice as hard as
    > writing the code in the first place. Therefore, if you write the code
    > as cleverly as possible, you are, by definition, not smart enough to
    > debug it.â€
    >
    > In other words, if it's not quickly obvious how to write a regular
    > expression that does what you want, use something else. Your
    > maintenance programmers (often your six-month older self) will thank
    > you.
    >
    > From the description you've given of the problem, I would think that
    > your best bet is to parse your input as a CSV, do the required
    > changes, and write it back into a CSV. Easier to write and easier to
    > read than a dose of line noise.


    It depends somewhat on the context.

    If the code is part of software where regex is used and it
    therefore is a fair assumption that the maintenance programmer
    knows regex, then regex is a fine solution.

    If regex is a well known as ancient greek, then it is not
    so smart.

    Arne
     
    Arne Vajhøj, Jul 24, 2012
    #12
  13. Jimmy

    Arne Vajhøj Guest

    Re: need regular expression to replace part of result based on asearch pattern

    On 7/12/2012 11:24 AM, Eric Sosman wrote:
    > "Some people, when confronted with a problem, think `I know,
    > I'll use regular expressions.' Now they have two problems."
    > -- Jamie Zawinski


    :)

    But the same can be said about many technologies.

    Arne
     
    Arne Vajhøj, Jul 24, 2012
    #13
  14. Jimmy

    Arne Vajhøj Guest

    Re: need regular expression to replace part of result based on asearch pattern

    On 7/23/2012 10:48 PM, Arne Vajhøj wrote:
    > On 7/11/2012 1:35 PM, Jimmy wrote:
    >> Have been having hard time to come up with the regular expression to
    >> replace the following..
    >>
    >> 1,"blaha",NULL,'N','Y','N','N',NULL
    >> 2,"blahb",NULL,'Y','Y','Y','N',NULL
    >> 3,"blahc",NULL,'N','N','N','N',NULL
    >> ^
    >>
    >> With all contents in column #6 (first column is column #1) to 'Y'?
    >>
    >> i.e. result
    >>
    >> 1,"blaha",NULL,'N','Y','Y','N',NULL
    >> 2,"blahb",NULL,'Y','Y','Y','N',NULL
    >> 3,"blahc",NULL,'N','N','Y','N',NULL

    >
    > Code for inspiration:
    >
    > import java.util.regex.Pattern;
    >
    > public class July {
    > private static final String fmt = "(%s(,%s){4},)(%s)((,%s){2})";
    > private static final String col1 = "[^,]*";
    > private static final Pattern pat1 =
    > Pattern.compile(String.format(fmt, col1, col1, col1, col1));
    > public static String reReplace1(String s) {
    > return pat1.matcher(s).replaceAll("$1'Y'$4");
    > }
    > private static final String col2 =
    > "(\\d+|(\"[^\"]*\")|('[^']*')|(NULL))";
    > private static final Pattern pat2 =
    > Pattern.compile(String.format(fmt, col2, col2, col2, col2));
    > public static String reReplace2(String s) {
    > return pat2.matcher(s).replaceAll("$1'Y'$16");
    > }


    Improved:

    private static final String fmt = "((%s,){5})(%s)((,%s){2})";
    private static final String col1 = "[^,]*";
    private static final Pattern pat1 = Pattern.compile(String.format(fmt,
    col1, col1, col1));
    public static String reReplace1(String s) {
    return pat1.matcher(s).replaceAll("$1'Y'$4");
    }
    private static final String col2 = "(\\d+|(\"[^\"]*\")|('[^']*')|(NULL))";
    private static final Pattern pat2 = Pattern.compile(String.format(fmt,
    col2, col2, col2));
    public static String reReplace2(String s) {
    return pat2.matcher(s).replaceAll("$1'Y'$12");
    }

    > public static void test(String s) {
    > System.out.println(s);
    > System.out.println(reReplace1(s));
    > System.out.println(reReplace2(s));
    > }
    > public static void main(String[] args) {
    > test("1,\"blaha\",NULL,'N','Y','N','N',NULL");
    > test("2,\"blahb\",NULL,'Y','Y','Y','N',NULL");
    > test("3,\"blahc\",NULL,'N','N','N','N',NULL");
    > test("4,\"blahd,nasty\",NULL,'N','N','N','N',NULL");
    > }
    > }


    Arne
     
    Arne Vajhøj, Jul 25, 2012
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    443
  2. lucky
    Replies:
    0
    Views:
    344
    lucky
    Feb 9, 2005
  3. Michael Tan
    Replies:
    32
    Views:
    1,024
    Ara.T.Howard
    Jul 21, 2005
  4. Replies:
    20
    Views:
    261
    Mark Lawrence
    Jun 15, 2013
Loading...

Share This Page