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

J

Jimmy

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
 
R

Roedy Green

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.
 
K

Knute Johnson

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);
}
}
 
D

Daniele Futtorovic

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.
 
R

Roedy Green

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.
 
J

Jimmy

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
 
E

Eric Sosman

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
 
J

Joshua Cranmer

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.
 
G

Gene Wirchenko

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
 
G

glen herrmannsfeldt

(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
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

"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
 
A

Arne Vajhøj

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top