Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10162

Re: Check if String.matches() AND (if yes) extract number from String?

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: Check if String.matches() AND (if yes) extract number from String?
References <4eca6890$0$6569$9b4e6d93@newsspool3.arcor-online.net> <replaceAll-20111121203555@ram.dialup.fu-berlin.de>
Message-ID <_Rxyq.24314$am1.10438@newsfe05.iad> (permalink)
Date 2011-11-21 11:43 -0800

Show all headers | View raw


On 11/21/11 11:36 AM, Stefan Ram wrote:
> jochen2@brenz.com (Jochen Brenzlinger) writes:
>> if var.matches("\\w*\[\\d+\]") {
>>    long l = ????;  }
>> ...but I have no idea on how to extract the number.
>
>    When you already know that »var« does match, you can use:
>
> java.lang.Long.valueOf( var.replaceAll( "\\D+(\\d+).", "$1" ))
>
>    .
>
Technically correct specific to this example.  However, if you have a 
more complicated pattern it won't necessarily work.

For example, \w*\[\d+\](?:\w+(\d+))?\w*(\d+)

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Check if String.matches() AND (if yes) extract number from String? jochen2@brenz.com (Jochen Brenzlinger) - 2011-11-21 15:04 +0000
  Re: Check if String.matches() AND (if yes) extract number from String? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-11-21 07:33 -0800
  Re: Check if String.matches() AND (if yes) extract number from String? Tassilo Horn <tassilo@member.fsf.org> - 2011-11-21 16:45 +0100
  Re: Check if String.matches() AND (if yes) extract number from String? Henk van Voorthuijsen <voorth@xs4all.nl> - 2011-11-21 07:39 -0800
  Re: Check if String.matches() AND (if yes) extract number from String? Roedy Green <see_website@mindprod.com.invalid> - 2011-11-21 10:26 -0800
  Re: Check if String.matches() AND (if yes) extract number from String? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-11-21 11:43 -0800

csiph-web