pattern matching help on logical OR

S

Shawna

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
 
J

John C. Bollinger

Shawna said:
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.

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?]|[$]",
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
 
S

Shawna

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
 
J

John C. Bollinger

Shawna said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top