• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expressions retrieval in java  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have saved some regular expressions in Oracle database and retreiving them in my java code.

For e.g., "ABC\\d{2}" when comes to java after retrieval comes as String str = "ABC\\\\d{2}" and length of both are not same. Hence when I use scanner object to find this pattern in my input string, it is not able to find.
But if I do system.out.println(String from data base); I get output as ABC\\d{2}.

What I think is java treats special characters like back slash in different way and to add escape sequence my string is modified and Scanner.findInLine(pattern) returns null object.

Length of both strings I mean whatever comes from dB and original are not same though while debugging and while printing output on console both looks same.

WorkAround- I tried saving pattern as "ABC\d{2}" in database and when it came to java it came as "ABC\\d{2}" and hence my code worked.

Please let me know, if there is a better way to do it or if some other approach is there. I want to save correct pattern "ABC\\d{2}" in database and then retrieve it.


Couldn't share my code because of security concerns, please let me know, if you require more information to resolve this.

Thanks
 
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a common problem when you run an escape sequence through more than one parser. That's the reason Java needs 2 back slashes for it.

You can easily get into situations which require 6 or more backslashes in Unix shells. I hate it.

I don't use Oracle but MySql.

Does your Oracle driver have a call to something like "unescape"? It may be that you are escaping the string (perhaps automatically) before writing but not after reading.

Joe
 
Gaurav Kr. Arora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Areeda wrote:This is a common problem when you run an escape sequence through more than one parser. That's the reason Java needs 2 back slashes for it.

You can easily get into situations which require 6 or more backslashes in Unix shells. I hate it.

I don't use Oracle but MySql.

Does your Oracle driver have a call to something like "unescape"? It may be that you are escaping the string (perhaps automatically) before writing but not after reading.

Joe


I am not aware of the oracle driver..will check that...but I couldn't understand the unix shell example quoted by you. Actually, I am writing the program on windows machine using eclipse but finally the program will run in unix machine. Both the machines will access the same Oracle database.
So, could you please tell me, if I put some regular expressions in Oracle, will they be read differently by java on two different operating systems?

Thanks.
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Kr. Arora wrote:
I am not aware of the oracle driver..will check that...but I couldn't understand the unix shell example quoted by you. Actually, I am writing the program on windows machine using eclipse but finally the program will run in unix machine. Both the machines will access the same Oracle database.
So, could you please tell me, if I put some regular expressions in Oracle, will they be read differently by java on two different operating systems?

Thanks.

The unix shell is just a common situation where you have more than one parser escaping sequences. To get a little more detailed:
The Pattern object looks for the string "\d" to specify a digit. The reason you have to put "\\d" in your Java source code is the Java parser would turn \d into an (illegal) escape code so you "escape" the backslash, so that "\\" is interpreted as "\". Oracle does the same thing so to get the 2 backslashes that Java needs into the string it escapes each backslash giving you "\\\\".

I don't know Oracle well enough to say for sure but I doubt very much that this would behave differently on Windows and Unix. It's too common.

Joe
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!