AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

pattern matching help on logical OR

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Shawna
Guest





PostPosted: Thu May 26, 2005 7:53 pm    Post subject: pattern matching help on logical OR Reply with quote



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,blahblahblahu007F";
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

Back to top
John C. Bollinger
Guest





PostPosted: Thu May 26, 2005 8:51 pm    Post subject: Re: pattern matching help on logical OR Reply with quote



Shawna wrote:

Quote:
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,blahblahblahu007F";
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.

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?

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

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

--
John Bollinger
[email]jobollin (AT) indiana (DOT) edu[/email]

Back to top
Shawna
Guest





PostPosted: Fri May 27, 2005 4:16 am    Post subject: Re: pattern matching help on logical OR Reply with quote



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

Back to top
John C. Bollinger
Guest





PostPosted: Fri May 27, 2005 2:59 pm    Post subject: Re: pattern matching help on logical OR Reply with quote

Shawna wrote:

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

--
John Bollinger
[email]jobollin (AT) indiana (DOT) edu[/email]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.