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


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

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.palinacquisition!news.posted.palinacquisition.POSTED!not-for-mail
NNTP-Posting-Date Mon, 21 Nov 2011 09:33:44 -0600
Date Mon, 21 Nov 2011 07:33:43 -0800
From Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.5; rv:8.0) Gecko/20111105 Thunderbird/8.0
MIME-Version 1.0
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>
In-Reply-To <4eca6890$0$6569$9b4e6d93@newsspool3.arcor-online.net>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Message-ID <G9qdnRszctfF8lfTnZ2dnUVZ_q2dnZ2d@posted.palinacquisition> (permalink)
Lines 25
X-Usenet-Provider http://www.giganews.com
NNTP-Posting-Host 50.46.118.188
X-Trace sv3-Pi1qOd3dC/eCP+QVWLqMiWNtriS5ypVrtn150JuM5FwGftLcy38yVngA/D98oojNi2/fR2N0DvlwnPo!mS8/ouLbU7oV6WisB83l2jkOUIcoJ6jP1YICuDMu6aXE3m3wsp1G2flJjpmyk9KZdIioeMAG7dKF!mOCgBZjW//LZ7sHY/4bZlOiGrrG7WllyB8oe4TQvQnI=
X-Complaints-To abuse@iinet.com
X-DMCA-Complaints-To abuse@iinet.com
X-Abuse-and-DMCA-Info Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info Otherwise we will be unable to process your complaint properly
X-Postfilter 1.3.40
X-Original-Bytes 2342
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10149

Show key headers only | View raw


On 11/21/11 7:04 AM, Jochen Brenzlinger wrote:
> Assume I have a String var and value like:
>
> String var = new String("foobar[345]");
>
> Now I want to check if this string matches a certain pattern and if yes extract the number into a long var.
> The first part is easy:
>
> if var.matches("\\w*\[\\d+\]") {
>     long l = ????;  }
>
> ....but I have no idea on how to extract the number.
> How can this be achieved?

I believe that if you want detailed information about the actual 
matches, you need to use either the Scanner or Matcher class, rather 
than the limited regex functionality of the String class.
http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html
http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html

I'm more accustomed to .NET regex handling, but either of those classes 
appear to provide the same match-group identification features you are 
looking for and which are available in .NET's Regex class.

Pete

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next 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