• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need a Regular Expression pattern in Java  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks in advance for your help. I have to replace a string if it's not available inside any single quote.

e.g.
name = 'abc12namexyz234' or name='def234namewsr345name' and name like '%ab123name345rt%'

In the above I've to replace "name" with "ApplicantEntity.name" but I don't want to replace the string that is available inside the single quote i.e. 'abc12namexyz234'. The result after the replacement should be:

ApplicantEntity.name= 'abc12namexyz234' or ApplicantEntity.name='def234namewsr345name' and ApplicantEntity.name like '%ab123name345rt%'

I am trying to find a appropriate regex. But still no success on that. Please Help....

Thanks,
Utpal
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why regex? Looks pretty straight forward.
 
utpal patra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. But how about " name = " (check for the space character) & "name like" (here instead of "=" "like" is available)
Also I want as follows:
str.replace("name = 'abc12namexyz234' or name='def234namewsr345name' and name like '%ab123name345rt%'", "ApplicantEntity.name");
In the above it will search for the lone string "name" and replace.
 
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Why regex? Looks pretty straight forward.


What happens if the bit in the quotes following the "name=" contains "name=" ? I know, since this is probably SQL being edited, it is not going to happen but ...
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
utpal patra wrote:Hi,
I am trying to find a appropriate regex. But still no success on that. Please Help....


The regex is not that difficult and I could post one that meets the requirement but the folks here do like to see some effort from you so they would probably remove my regex. What have you got so far?
 
utpal patra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Richard,
I've tried with the followings:

1. [ ]*[=]+ --> Only for "="
2. [ ]*[=]+|[like]+ --> For "=" and "like"

But problem here is while using str.replaceAll(regex, replacement) the occurrence of "=" and "like" is random. So it's not working perfectly.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
utpal patra wrote:But problem here is while using str.replaceAll(regex, replacement) the occurrence of "=" and "like" is random. So it's not working perfectly.

Well, I think your first mistake is to try to do this all in one go. Why not instead:
1. Break up your string into "stuff inside single quotes" and "stuff outside single quotes".
2. Only do your replacement in the "stuff outside single quotes".

Second: Quotation rules are notoriously tricky, especially for regular expressions. For example, what happens if your "string" contains a single quote itself, eg:
name = 'abcCharlie's12namexyz234'
?

That'll throw off pretty much any regex, unless you have clearly defined rules for such a situation.

Winston
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My simply tested approach is

which does seem to handle Winston's quotes within quotes point.

If you think of using this you should write some good JUnit tests trying to cover all the edge conditions.
 
utpal patra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much appreciated response. Thanks. Let me try it out by using StringTokenizer.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
utpal patra wrote:Much appreciated response. Thanks. Let me try it out by using StringTokenizer.


Err... " StringTokenizer" . How does that come into things?
 
utpal patra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Richard. It's working fine. Could you please do a little favor by explaining about the pattern as I am very new to regex ?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
utpal patra wrote:Thanks Richard. It's working fine. Could you please do a little favor by explaining about the pattern as I am very new to regex ?

 
utpal patra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Richard.
Now I've used
"name\\s*(?:=|!=|(?i)LIKE|>|>=|<|<=)\\s*'[^']*'" for all the other sql expression like <, <=. But it is not working for <=, >= etc. Please suggest.
 
utpal patra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the wrong query posted above. Actually the above pattern is working for "name >= '100'". But not working for "name >= 100". I agree this is due to the single quote pattern. Do you have any suggestion how to handle this kind of situation ?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
utpal patra wrote:Sorry for the wrong query posted above. Actually the above pattern is working for "name >= '100'". But not working for "name >= 100". I agree this is due to the single quote pattern. Do you have any suggestion how to handle this kind of situation ?


Replace the "quote term" by a "quote term OR sequence of decimals" and please lay it out as I did since it makes it maintainable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!