• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help in removing trailing and leading zeroes using regex  RSS feed

 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the following program to remove leading and trailing zeroes from a string using regex.



Without the 8th line, the output is



And with it the output is.



I do not understant why the string becomes null in the second case even when m.matches() returns true. I think that the pattern says, "match zero or more occurances of a character that is not zero, then match one or more occurances of any digit and again match zero or more occurances of a character that is not zero". If I am wrong then please tell me the right way to do it.
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first case, you are finding from the beginning. In the second case, you are finding from where the matches() method left off. If you want to (from your loop) use the find() method from the beginning of the string, then don't forget to reset the matcher.

Henry
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Henry. It is the right way to remove leading and trailing zeroes, right?
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rrohit rakesh upadhyay wrote:Thank you, Henry. It is the right way to remove leading and trailing zeroes, right?


Considering that you got a result of "00012345000", which has both leading and trailing zeroes... I would say "no". Your technique didn't work.

Henry
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i remove '*' with '+' i am getting the result right but the m.matches() returns false.
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rrohit rakesh upadhyay wrote:When i remove '*' with '+' i am getting the result right but the m.matches() returns false.


Yeah. I think you will need to provide more context ... TellTheDetails.

Henry
 
Knute Snortum
Sheriff
Posts: 4207
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if we still are supposed to post hints instead of full solutions since this is now in Java in General, but I will say that I have a one-line solution that I will post if I get the go-ahead. Until then, think about using the method replaceFirst(). It's a little confusing because of the "first" in the name, but it's a way to use a regex to replace something in a string ("first" because it replaces only the first instance of it, but we don't care).
 
Knute Snortum
Sheriff
Posts: 4207
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, here's the code I would use to strip off trailing and leading zeros:

 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alternatively, the replaceAll() method can also be used, with a merged regex (using the alternation operator).

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!