Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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.

Reply With Quote
  #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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:15 AM.


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