I'm trying to make a java project where i will be taking a log file (txt file) of data entries and a regular expression file (also txt and contains RE rules). My goal is to take each rule (RE) and run it through the log file and find matches, the end goal is to print the number of matches i found. The code is supposed to be generic meaning that i dont care what the regular expressions are, i just a function that takes 2 parameters, a regular expression and a string line from the log file. I found out that i can use the regex class (using the pattern and matcher class). So my program consists of 2 nested for loops and the steps are as follows: 1- store first regular expression from the rules file. 2- compare the rule with every line in the log file using the pattern and matcher class. 3- if a match is found i increment a counter. nb: aside from the nested loops. my problem is if for example the rule is "notifications" and the log line is "[INFO ] 2020-05-28 09:13:38.965 notifications [main] [CommonEventsManager.initialize] - Initialization for module [Common] was successful..." i should get a match but the result is false. this is the code im using to match :
- Pattern p = Pattern.compile("notifications");
- Matcher m = p.matcher( "[INFO ] 2020-05-28 09:13:38.965 notifications [main] - [CommonEventsManager.initialize] - Initialization for module [Common] was successful..."); -System.out.println("dsds "+ m.matches());
So how can i use regex to find matches without having to worry about the regular expression format