Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > need regular expression to replace part of result based on a search pattern

Reply
Thread Tools

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

 
 
Arne Vajhj
Guest
Posts: n/a
 
      07-24-2012
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


 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      07-24-2012
On 7/12/2012 10:42 AM, Leif Roar Moldskred wrote:
> Jimmy <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Arne Vajhj
Guest
Posts: n/a
 
      07-24-2012
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


 
Reply With Quote
 
Arne Vajhj
Guest
Posts: n/a
 
      07-25-2012
On 7/23/2012 10:48 PM, Arne Vajhj 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Import Images from XML & Replace Text Based on Regular Expression sherazam Java 0 11-05-2010 09:35 AM
Search, Replace with Regular Expression tushar.n.patel@gmail.com ASP .Net 1 02-09-2005 10:09 PM
Search, Replace with Regular Expression lucky ASP .Net 0 02-09-2005 07:10 AM
regular expression search replace J. VerSchave Java 2 02-29-2004 08:08 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments