Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
| Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics 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
  #1 (permalink)  
Old July 1st, 2008, 12:14 PM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default New help with Pattern in Java

Hi.

I am working with Pattern and Matcher in Java.
I need help creating a pattern to match the following strings:

    MAIL FROM:
    MAIL FM:
    MAIL FROM:

This is what I have on my pattern, "MAIL\\s+FR{0,1}O{0,1}M:"

My pattern seem to work if the string is "MAIL FRM:" or "MAIL FOM:", which is incorrect.

Please help.

  #2 (permalink)  
Old July 1st, 2008, 04:40 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Well, sure. The pattern
Code:
     "MAIL\\s+FR{0,1}O{0,1}M:"
says:
Code:
MAIL == literally
\\s+ == one or more spaces
F == literally
R{0,1} == an optional letter R
O{0,1} == an optional letter O
M: == literally
There are a couple of ways to solve this:
Code:
     "MAIL\\s+F(RO)?M:"
That makes the *group* RO optional. And that certainly works.

But my own preference would be K.I.S.S.
Code:
     "MAIL\\s+(FM|FROM)M:"
That says "FM" or "FROM" as choices.

p.s.: No reason to ever use {0,1} when ? means the same thing.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ajax Pattern TheGame Ajax 1 July 26th, 2008 03:02 PM
Javascript Pattern How To muklee Javascript How-To 3 September 15th, 2006 09:11 AM
design pattern kishore.dyn Struts 1 February 14th, 2006 02:36 PM
Desing Pattern tilakkalyan J2EE 3 February 14th, 2006 07:04 AM
DirectoryInfo.GetFiles(pattern): search pattern fo arif_1947 VS.NET 2002/2003 1 October 19th, 2004 11:59 PM





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