• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Reg Expressions

 
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a field that contains a value of 1234AbcDE1234 , what would be the regular expression ro remove the leading 1234. An example would be helpful. I have serached the web and can't find this type of an example. I'm now reading the tutorial/essential/regex from the oracle java web site, but its complicated and I need an exampe to help me along. Thank you
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regex is overkill for removing the first 4 characters of a String. Use substring(...)
 
Joe Brigs
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I misspoke. The value in this field varies ( comes from a database ), it could be anything but I will always have to drop the leading numbers. ex => 12Some32 or 78999999somethingelse456 or 567reRE12 . Not sure if I can use substring if the pattern varies like this. ??? Thanks for reply
 
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you take a look at the Javadoc page of java.util.regex.Pattern, you can find:
- how to specify "starts with" in regular expressions.
- how to specify one single digit in regular expressions.
- how to specify zero or more of anything in regular expressions.

Combine these three, use a Pattern and Matcher, and you can possibly find() a match. If so, strip away that match - using substring as Darryl suggested. The length of the match is the number of characters to strip away.
 
Marshal
Posts: 80761
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's also a good section about regular expressions in the Java™ Tutorials.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And another good learning resource is http://www.regular-expressions.info/


edit

Combine these three, use a Pattern and Matcher, and you can possibly find() a match. If so, strip away that match - using substring as Darryl suggested. The length of the match is the number of characters to strip away.


Or replaceAll(regexThatMatchesTheStartOfInputFollowedByAnyNumberOfDigits, ""). No need for substring().
 
Rob Spoor
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, a definite +1 on that one
 
Joe Brigs
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will do the reading/reserach today on your suggestions and give it a try. Thanks evryone
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:And another good learning resource is http://www.regular-expressions.info/


edit

Combine these three, use a Pattern and Matcher, and you can possibly find() a match. If so, strip away that match - using substring as Darryl suggested. The length of the match is the number of characters to strip away.


Or replaceAll(regexThatMatchesTheStartOfInputFollowedByAnyNumberOfDigits, ""). No need for substring().



1) Since the OP only wants to replace the start of the line he would do better to use replaceFirst().

2) This uses Pattern and Matcher behind the scenes and the Pattern will be parsed every time it is used. Obviously this will have very little impact for a few calls but pre-compiling the Pattern can make quite a big difference if millions of lines are being processed.

3) This will not report any error if there is a line that does not have the leading decimal digits. This may or may not matter to the OP but ...
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just want to point something out...

You don't use a regular expression (regex) "to remove" anything. A regex is used to MATCH something (or find something that doesn't match). So what you are looking for is:

1) a regex to match 'the beginning of a line and 0 or more digits'.
2) a way to remove that from a string.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic