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


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

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.erje.net!news-1.dfn.de!news.dfn.de!cache.uni-koblenz.de!not-for-mail
From Tassilo Horn <tassilo@member.fsf.org>
Newsgroups comp.lang.java.programmer
Subject Re: Check if String.matches() AND (if yes) extract number from String?
Date Mon, 21 Nov 2011 16:45:56 +0100
Organization University Koblenz-Landau Campus Koblenz
Lines 28
Message-ID <87ty5xlbrf.fsf@tsdh.uni-koblenz.de> (permalink)
References <4eca6890$0$6569$9b4e6d93@newsspool3.arcor-online.net>
NNTP-Posting-Host tsdh.uni-koblenz.de
Mime-Version 1.0
Content-Type text/plain
X-Trace cache.uni-koblenz.de 1321890367 23513 141.26.67.142 (21 Nov 2011 15:46:07 GMT)
X-Complaints-To news@cache.uni-koblenz.de
NNTP-Posting-Date Mon, 21 Nov 2011 15:46:07 +0000 (UTC)
User-Agent Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.91 (gnu/linux)
Cancel-Lock sha1:TpBSQpi81bApFe6WDAMPZtYfPJw=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10150

Show key headers only | View raw


jochen2@brenz.com (Jochen Brenzlinger) writes:

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

You are looking for Capturing Groups.  Have a look at
java.util.regex.Pattern and Matcher.  You need something along these
lines (untested):

        String foo = "bla[123]";
        Pattern myPattern = Pattern.compile("\\w+\\[(\\d+)\\]");
        Matcher m = myPattern.matcher(foo);
        if (m.find()) {
                long idx = Long.parseLong(m.group(1));
                // idx should be 123 here
        }

Bye,
Tassilo
-- 
(What the world needs (I think) is not
      (a Lisp (with fewer parentheses))
      but (an English (with more.)))
Brian Hayes, http://tinyurl.com/3y9l2kf

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