• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Need help in removing trailing and leading zeroes using regex

 
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Henry. It is the right way to remove leading and trailing zeroes, right?
 
Henry Wong
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i remove '*' with '+' i am getting the result right but the m.matches() returns false.
 
Henry Wong
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, here's the code I would use to strip off trailing and leading zeros:

 
Henry Wong
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic