Question on Pattern Matcher?

I

IchBin

Given the following code snippet. Why would I not get a pattern match
from a JTree Selection?

( Code Snippet )____________________________
System.out.println(selectedIP.substring(0,(selectedIP.indexOf("["))
-1).trim());

TreePath currentPath = tree.getSelectionPath();
TreePath parentPath = currentPath.getParentPath();
System.out.println(parentPath.toString());

Pattern openPattern = Pattern.compile("Opens");
System.out.println("openPattern: "+openPattern);
Pattern closePattern = Pattern.compile("Closes");
System.out.println("closePattern: "+closePattern);
Pattern dropPattern = Pattern.compile("Drops");
System.out.println("dropPattern: "+dropPattern);

CharSequence inputStr = parentPath.toString();
System.out.println("CharSequence: "+inputStr);

Matcher openMatcher = openPattern.matcher(inputStr);
System.out.println("openMatch: "+openMatcher.matches());

Matcher closeMatcher = closePattern.matcher(inputStr);
System.out.println("closeMatcher: "+closeMatcher.matches());

Matcher dropMatcher = dropPattern.matcher(inputStr);
System.out.println("dropMatcher: "+dropMatcher.matches());

( Display Output per/selection )____________________________
24.229.165.60
[UDP, IP Opens, 509 Records]
openPattern: Opens
closePattern: Closes
dropPattern: Drops
CharSequence: [UDP, IP Opens, 509 Records]
openMatch: false
closeMatcher: false
dropMatcher: false

24.229.165.60
[UDP, IP Closes, 489 Records]
openPattern: Opens
closePattern: Closes
dropPattern: Drops
CharSequence: [UDP, IP Closes, 489 Records]
openMatch: false
closeMatcher: false
dropMatcher: false


Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist
 
I

IchBin

Would I have to take a substring of the CharSequence of the pattern I am
looking for to have it match? that is say "Closes"?

CharSequence: [UDP, IP Closes, 489 Records]
--


Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist
 
H

hilz

Would I have to take a substring of the CharSequence of the pattern I am
looking for to have it match? that is say "Closes"?

CharSequence: [UDP, IP Closes, 489 Records]

try this:

Pattern.compile(".+Opens.+");

thanks
hilz
 
I

IchBin

hilz said:
Would I have to take a substring of the CharSequence of the pattern I am
looking for to have it match? that is say "Closes"?

CharSequence: [UDP, IP Closes, 489 Records]


try this:

Pattern.compile(".+Opens.+");

thanks
hilz
Thanks hilz.. That did it for me!

Guess I need to learn pattern matching!

IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances: if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist
 
A

Alan Moore

try this:

Pattern.compile(".+Opens.+");

Or stay with your original regexes, but use the find() method instead
of matches(). matches() returns true only if the regex describes the
whole input, while find() looks for a subsequence of the input that
matches.
 
I

IchBin

Alan said:
Or stay with your original regexes, but use the find() method instead
of matches(). matches() returns true only if the regex describes the
whole input, while find() looks for a subsequence of the input that
matches.

Thanks Alan...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances: if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,082
Members
48,773
Latest member
Kaybee

Latest Threads

Top