Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #15942 > unrolled thread

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

Started byJimmy <jimmy_please@yahoo.com>
First post2012-07-11 10:35 -0700
Last post2012-07-25 17:37 -0400
Articles 15 — 10 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  need regular expression to replace part of result based on a search pattern Jimmy <jimmy_please@yahoo.com> - 2012-07-11 10:35 -0700
    Re: need regular expression to replace part of result based on a search pattern Roedy Green <see_website@mindprod.com.invalid> - 2012-07-11 15:24 -0700
    Re: need regular expression to replace part of result based on a search pattern Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2012-07-11 15:40 -0700
      Re: need regular expression to replace part of result based on a search pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-07-12 00:56 +0200
    Re: need regular expression to replace part of result based on a search pattern Roedy Green <see_website@mindprod.com.invalid> - 2012-07-11 21:21 -0700
    Re: need regular expression to replace part of result based on a search pattern Jimmy <jimmy_please@yahoo.com> - 2012-07-12 07:28 -0700
      Re: need regular expression to replace part of result based on a search pattern Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-12 11:24 -0400
        Re: need regular expression to replace part of result based on a search pattern Joshua Cranmer <Pidgeot18@verizon.invalid> - 2012-07-12 11:42 -0400
        Re: need regular expression to replace part of result based on a search pattern Arne Vajhøj <arne@vajhoej.dk> - 2012-07-23 22:55 -0400
    Re: need regular expression to replace part of result based on a search pattern Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-07-12 09:42 -0500
      Re: need regular expression to replace part of result based on a search pattern Gene Wirchenko <genew@ocis.net> - 2012-07-12 09:18 -0700
      Re: need regular expression to replace part of result based on a search pattern glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-07-12 18:34 +0000
      Re: need regular expression to replace part of result based on a search pattern Arne Vajhøj <arne@vajhoej.dk> - 2012-07-23 22:53 -0400
    Re: need regular expression to replace part of result based on a search pattern Arne Vajhøj <arne@vajhoej.dk> - 2012-07-23 22:48 -0400
      Re: need regular expression to replace part of result based on a search pattern Arne Vajhøj <arne@vajhoej.dk> - 2012-07-25 17:37 -0400

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

FromJimmy <jimmy_please@yahoo.com>
Date2012-07-11 10:35 -0700
Subjectneed regular expression to replace part of result based on a search pattern
Message-ID<6aefda61-b66a-4e8f-8634-ef6a95f79c4d@googlegroups.com>
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

[toc] | [next] | [standalone]


#15951

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-07-11 15:24 -0700
Message-ID<64vrv7h56gk2k1smfkl9od1qdtnbibr195@4ax.com>
In reply to#15942
On Wed, 11 Jul 2012 10:35:05 -0700 (PDT), Jimmy
<jimmy_please@yahoo.com> 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.

[toc] | [prev] | [next] | [standalone]


#15954

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2012-07-11 15:40 -0700
Message-ID<jtkvc3$vu6$1@dont-email.me>
In reply to#15942
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[i]);
             if (i != arr.length - 1)
                 sb.append(",");
         }
         System.out.println(sb);
     }
}


[toc] | [prev] | [next] | [standalone]


#15959

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2012-07-12 00:56 +0200
Message-ID<jtl0bh$3d5$3@dont-email.me>
In reply to#15954
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[i]);
>             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.

[toc] | [prev] | [next] | [standalone]


#15974

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-07-11 21:21 -0700
Message-ID<r2ksv7pk9mi2q8fuihck6392ie32h1h8vo@4ax.com>
In reply to#15942
On Wed, 11 Jul 2012 10:35:05 -0700 (PDT), Jimmy
<jimmy_please@yahoo.com> 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.

[toc] | [prev] | [next] | [standalone]


#15980

FromJimmy <jimmy_please@yahoo.com>
Date2012-07-12 07:28 -0700
Message-ID<f92af690-9380-4885-9955-ff9b536613d6@googlegroups.com>
In reply to#15942
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

[toc] | [prev] | [next] | [standalone]


#15982

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-07-12 11:24 -0400
Message-ID<jtmq79$bcf$1@dont-email.me>
In reply to#15980
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
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#15983

FromJoshua Cranmer <Pidgeot18@verizon.invalid>
Date2012-07-12 11:42 -0400
Message-ID<jtmr91$iek$1@dont-email.me>
In reply to#15982
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

[toc] | [prev] | [next] | [standalone]


#16291

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-07-23 22:55 -0400
Message-ID<500e0eb1$0$282$14726298@news.sunsite.dk>
In reply to#15982
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

[toc] | [prev] | [next] | [standalone]


#15981

FromLeif Roar Moldskred <leifm@dimnakorr.com>
Date2012-07-12 09:42 -0500
Message-ID<VMednY1XR8zyf2PSnZ2dnUVZ8ridnZ2d@giganews.com>
In reply to#15942
Jimmy <jimmy_please@yahoo.com> 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.

-- 
Leif Roar Moldskred

[toc] | [prev] | [next] | [standalone]


#15984

FromGene Wirchenko <genew@ocis.net>
Date2012-07-12 09:18 -0700
Message-ID<ptttv71mt6jf2mog2p2fr7816am6gpka7l@4ax.com>
In reply to#15981
On Thu, 12 Jul 2012 09:42:55 -0500, Leif Roar Moldskred
<leifm@dimnakorr.com> wrote:

>Jimmy <jimmy_please@yahoo.com> 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

[toc] | [prev] | [next] | [standalone]


#15987

Fromglen herrmannsfeldt <gah@ugcs.caltech.edu>
Date2012-07-12 18:34 +0000
Message-ID<jtn5bs$891$1@speranza.aioe.org>
In reply to#15981
Leif Roar Moldskred <leifm@dimnakorr.com> 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

[toc] | [prev] | [next] | [standalone]


#16290

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-07-23 22:53 -0400
Message-ID<500e0e0d$0$282$14726298@news.sunsite.dk>
In reply to#15981
On 7/12/2012 10:42 AM, Leif Roar Moldskred wrote:
> Jimmy <jimmy_please@yahoo.com> 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

[toc] | [prev] | [next] | [standalone]


#16289

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-07-23 22:48 -0400
Message-ID<500e0d18$0$282$14726298@news.sunsite.dk>
In reply to#15942
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

[toc] | [prev] | [next] | [standalone]


#16364

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-07-25 17:37 -0400
Message-ID<50106712$0$282$14726298@news.sunsite.dk>
In reply to#16289
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web