GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Reg Expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reg Expressions" Watch "Reg Expressions" New topic
Author

Reg Expressions

Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 60

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

Joined: May 03, 2008
Posts: 4571
    
    5

Regex is overkill for removing the first 4 characters of a String. Use substring(...)


luck, db
There are no new questions, but there may be new answers.
Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 60

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
There's also a good section about regular expressions in the Java™ Tutorials.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4571
    
    5

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Yeah, a definite +1 on that one
Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 60

I will do the reading/reserach today on your suggestions and give it a try. Thanks evryone
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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 ...

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
Consider Paul's rocket mass heater.
 
subject: Reg Expressions