Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2005, 10:52 AM
Authorized User
 
Join Date: Jul 2004
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regular expression question - non-capturing groups

Hello.

I want to be able to extract a four digit number from a piece of text, which may or may not have another three-digit code preceding it.
For example, if the string was:

HHHH_9017564_UUP.txt

I would want to be able to extract "7564". "901" may or may not be there, so if I just searched for a four-digit number I would get "9017" some of the time.

I want to be able to check for the presence of "901", without returning it as part of the capture. How do I do this in a single regular expression?

Here is my code:

public static void main(String[] args)
{
  String sInput = args[0];
  String sRegex = "(?:901)?[0-9]{4}";
  Pattern p = Pattern.compile(sRegex);
  Matcher m = p.matcher(sInput);
  System.out.println(m.matches());
  while (m.find())
  {
    System.out.println(sInput.substring(m.start(), m.end()));
  }
  System.out.println("No more matches to find.");
}

I though (?:X) was a non-capturing group, but maybe this doesn't do what I though it did?

Chris Jarvis





Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression Question bonekrusher XSLT 4 April 17th, 2007 06:58 AM
Regular expression question cridley General .NET 1 May 28th, 2005 01:14 PM
Regular Expression Help anshul PHP How-To 4 December 8th, 2004 05:19 AM
question regarding regular expression validator NewbieProg Classic ASP Basics 1 November 9th, 2004 08:49 AM
regular expression Moharo Pro PHP 3 April 30th, 2004 04:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.