Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2005

Tip: Looking for answers? Try searching our database.

pattern matching help on logical OR

Thread view: 
Shawna - 26 May 2005 20:53 GMT
I'm using Java 1.4.2_05 and need some help getting a logical OR to work
within my pattern matcher.

I'm trying to get both string A and B to be recognized by one pattern?
    String strA = "04/14:03,blahblahblah\u007F";
    String strB = "04/14:03,blahblahblah";

So I'm trying to match:
Two numerals followed by slash followed by 2 numerals followed by a
colon followed by 2 numerals followed by a comma followed by anything
followed by a one or 0 delete character OR the end of the line/string.

Here is the regex:
Pattern.compile("[0-9]{2}[\u002F][0-9]{2}[\u003A][0-9]{2}[\u002C].+[\u007F?]|[$]",
Pattern.DOTALL);

The logical OR doesn't work though.  And I've tried the OR a few other
ways as well.
... [\u007F|$]
... [\u007F?|$]
... [\u007f]?|[$]
These don't work either.

I'd appreciate any suggestions.

Thanks,
Shawna
John C. Bollinger - 26 May 2005 21:51 GMT
> I'm using Java 1.4.2_05 and need some help getting a logical OR to work
> within my pattern matcher.
[quoted text clipped - 7 lines]
> colon followed by 2 numerals followed by a comma followed by anything
> followed by a one or 0 delete character OR the end of the line/string.

So how is the end of the String special in this regard?  What is
difference between the case where there is no \u007f and when you want
to match end-of-input?

The two specific strings you provided are both matched by this pattern:

    "[0-9]{2}/[0-9]{2}:[0-9]{2},.+\\u007f?"

(without necessity of any regex options).  Are there other strings that
need to be matched by the pattern but aren't?

> Here is the regex:
> Pattern.compile("[0-9]{2}[\u002F][0-9]{2}[\u003A][0-9]{2}[\u002C].+[\u007F?]|[$]",
[quoted text clipped - 6 lines]
> ... [\u007f]?|[$]
> These don't work either.

When you put the $ in a character class, it represents a literal '$'
character, not end-of-input / end-of-line.  It is unlikely in your case
that you need to explicitly look for end-of-line at all.

You may find this regex testing tool useful:
http://www.fileformat.info/tool/regex.htm

Signature

John Bollinger
jobollin@indiana.edu

Shawna - 27 May 2005 05:16 GMT
Thanks John.  An alarm is coming in from some switch equipment via a
modem.  My stuff reads the characters from on a socket.  Usually the
alarms are terminated with a delete character, but not always.  So I'm
looking for some sort of end of string as well as the delete.  However,
in the cases without a delete, I may have to just have to rely on a
time-out.

Shawna
John C. Bollinger - 27 May 2005 15:59 GMT
> Thanks John.  An alarm is coming in from some switch equipment via a
> modem.  My stuff reads the characters from on a socket.  Usually the
> alarms are terminated with a delete character, but not always.  So I'm
> looking for some sort of end of string as well as the delete.  However,
> in the cases without a delete, I may have to just have to rely on a
> time-out.

By the time you match the String against the pattern you must have
already parsed it from the input, so the question of matching a message
boundary is at a lower level.  Are you saying, though, that when there
is a delete character in the input, you can tolerate other, unspecified
data afterward?  You could write that into the pattern, but it might be
more appropriate to look into the differences in behavior between
Matcher.matches(), Matcher.lookingAt(), and Matcher.find().  One of the
latter two may serve your purpose better than the first.

Also, I didn't previously say so, but you probably want to construct an
explict Pattern object and use Matchers based on it, rather than using
String.matches().  Not only does doing so give you more flexibility, but
if you hold on to the Pattern object and reuse it then you also save the
work of parsing the pattern string for every match attempt.

Signature

John Bollinger
jobollin@indiana.edu



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.