Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with a regex

 
B Mayes
Ranch Hand
Posts: 47
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic