aspose file tools*
The moose likes Java in General and the fly likes Problem with a regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with a regex" Watch "Problem with a regex" New topic
Author

Problem with a regex

B Mayes
Ranch Hand

Joined: Apr 28, 2010
Posts: 47

Hi everyone. I am testing out some code that parses logs for all sorts of information, though one of the main features is to look for error messages and provide solutions. I have recently implemented a feature that allows users to enter an error message as a regular expression. It seems to work for some simple regular expressions, but I'm having problems with ones that are a little more complex. For example, I want to find a line in the long that contains one of the following messages:

port sharing unavailable: ip.tcp.http:1920

port sharing unavailable: ip.ssl.https:3661


I have put together the following regular expressions but none of them seem to work. Note that I am printing the output of the string as it gets printed by System.out.println. Yes -- I realize that if I am typing it directly as a String variable in Java that I need to escape backslashes. So the expression \w actually needs to be typed as "\\w". I get that...but the output here is actually what is getting printed by my debug statements (showing the value of the regex AFTER I have unescaped the backslashes and turned it into a "normal" string again that Pattern can use).and so on. What am I missing here? I'm sure it's something silly but I just don't know what. It may also be useful to know that I'm using the Pattern.DOTALL option when I am compiling the pattern...
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Your first regex works fine (though it could be improved) so there must be something about your data that you are not telling us. Are you sure you don't have a leading or trailing space?

P.S. You don't need DOTALL since you are not trying to match end-of-line.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

First one works fine for me. What does your input look like, and how are you doing the matching?

Edit: what he said.
B Mayes
Ranch Hand

Joined: Apr 28, 2010
Posts: 47

So an example line in the log file looks like this:




I was trying to manually type the message in the log file from memory, but maybe I just didn't type something correctly. So I updated my error table in MySQL with the regex ip\.\w{3}\.https?:\d{4}. Then I pasted the actual error message (above) into a sample log and somehow it worked this time:





I am uh...not really sure what I changed but it seems to be working now so thanks!


The one thing I am certain of is that I need to provide a small utility in my program where you can enter a regular expression along with a log file, and then click a button to test that the regular expression actually works! Then users will be able to verify it is syntactically correct before committing it to a table in the database.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with a regex