Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg Expressions

 
Joe Brigs
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
  • 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
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also a good section about regular expressions in the Java™ Tutorials.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I will do the reading/reserach today on your suggestions and give it a try. Thanks evryone
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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 ...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic