• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expression in java  RSS feed

 
Swapna latha
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string as "Transfer 60.45 USD from India". In this one i need to replace 60.45 and USD as * using regular expression. I have written one, but failed in case sensitive.

What i have written is [\\d*\\.+\\d*][.*USD.*].

But this is not working perfectly with case sensitivity. I mean if i give usd then its not working.

thanks

swapna
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the APi for java.util.regex.Pattern for the case insensitive flag.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swapna latha wrote:But this is not working perfectly with case sensitivity. I mean if i give usd then its not working.

Which actually may be correct - especially if you're using ISO 4217. The actual code is "USD" and there is nothing in the Wiki pages that talks about "case-sensitive" (although it does mention 'letters'), which I would take to mean that codes could be introduced that use mixed (or lower) case in the future.

My advice: Don't overthink things. Or at least look at the actual text of the ISO 4217 standard before you start simply "allowing" lower-case alternatives.

Question: Is "USd" allowed? or "UsD"? Simply making everything case-insensitive will allow them all.

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What i have written is [\\d*\\.+\\d*][.*USD.*].


Well, the problem is, you are using a character class. you should first understand the basics of regular expressions. I would suggest you to start with - This tutorial.
A character class, as the name suggest, matches a single character from all the characters you use inside it. Also, any meta-character in regex don't have any special meaning inside character class. For e.g., "[\\d*]", doesn't match 0 or more digits. It really matches a single digit, or a `*`. So, effectively, it is same as: - "\\d|\\*".

So, the regex you used is equivalent to:


See, "[USD]" doesn't matches `USD`, it matches either `U`, or `S`, or `D`.

To solve your issue, you can try using this regex (but please don't just copy-paste, go through the tutorial I posted above - at least the basics):

And replace it with "*".
 
Swapna latha
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all, I have resolved the issue
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!