Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regExp pattern / matcher problem

Status
Not open for further replies.

ketandba

Programmer
Nov 15, 2004
38
US
hi,


I am using postgres 7.4.6 and java 1.4.

I have created a table, like
create table usr (usr_id varchar(15), usr_filename_pattern varchar(1024));

insert into usr values ('A', '(d*-d*-d)');
insert into usr values ('B', '(dd-dd-dd)');
insert into usr values ('C', '(d*-d*-d)|(dd-dd-dd)');

i.e. value of usr_id = 'A' and usr_filename_pattern = '(d*-d*-d)'

i want to replace
'(d*-d*-d)' with '(\\d*-\\d*-\\d)'
using pattern and matcher method of java.

what is the value of regExp for matching

String fileNamePattern = '(d*-d*-d)|(dd-dd-dd)';

pl. help me out...

Thanks in advanced..

ketan
 
The '\' is a special character in Java.
Where you mean explicitly '\', you have to escape it with a second '\'.
So "\\" has to be written as "\\\\".

Now regular expressions have a special masking-character too, and guess what it is?

Right. It's '\'.
To use a '\' in a pattern or replacement, you have to escape it again.

I guess you have to make your replaceString with "\\\\\\\\" for "\\".

But how will postgres interpret a backslash?
Does it use backslashes itself for masking?
Then you would need 16 slashes in front of each "d".
Code:
updCol = column.replaceAll ("(d*-d*-d)", "(\\\\\\\\\\\\\\\\d*-\\\\\\\\\\\\\\\\d*-\\\\\\\\\\\\\\\\d)");

seeking a job as java-programmer in Berlin:
 
hi,
stefanwagner's,

Thanks for reply,

You are right..i am replacing '\\' with '\\\\\\\\'.

But when i usr regExp = '(d*-d*-d)', it can't match to original string.. i.e matches() = false and my replacement not happend.

here is my snippet...

Code:
	String fnp =null;
	//String regExp = "\\(\\d*-\\d*-\\d)";
        String regExp = "(d*-d*-d)";

			Pattern p1 = Pattern.compile(regExp);
			//p1.matches(filenamePattern,"d*");
			Matcher m1 = p1.matcher(filenamePattern);
			
			System.out.println("FilenamePattern ==> " + filenamePattern );
			System.out.println("regExp ==> " + regExp);
			if (m1.matches()) {
				System.out.println("pattern match for replacement");
				System.out.println("FilenamePattern ==> " + filenamePattern );
				System.out.println("regExp ==> " + regExp);
				fnp = m1.replaceAll("(\\\\\\\\d*-\\\\\\\\d*-\\\\\\\\d)");
			}
			else{
				System.out.println("pattern does not match");
			}

			
			System.out.println("fnp ++====> " + fnp);

pl. help me...

thanks ones again...
Your help is appreciated.

ketan
 
Yes, well, * is a special character in regular expressions too.

We have to escape it with a \.
But java will consume a \ and think, the next character is meaning something special like \t, \n, \\ so we use "\\*" instead of "*".

String regExp = "(d\\*-d\\*-d)";

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top