epifanio67
asked on
Java: regular expression: how do I search in multiple tabbed lines?
Hello Experts,
My question embedded in code below:
In advance, thank you for your help...
My question embedded in code below:
String dnis = "0027";
//I want my regular expression to also match the tabbed line containing
//the DNIS matching the string above. How do I do this?
Pattern p = Pattern.compile("^*?(\\@\\d+:\\d+:\\d+)(.*SipReg)");
//ANI and DNIS are tabbed in logs
String file = "@13:06:10 SipReg to switch" + "\n"
+ " ANI '2145558877' " + "\n"
+ " DNIS '0027' " + "\n"
+ "@13:07:11 SipReg to switch " + "\n"
+ " ANI '9726668877'" + "\n"
+ " DNIS '1127' " + "\n";
Matcher matcher = p.matcher(file);
while (matcher.find() == true)
System.out.println(matcher.group());
In advance, thank you for your help...
ASKER
Thank you expert,
sure, my apologies for being vague....
I want my regular expression to:
search for time at the beginning of the line - ^*?(\\@\\d+:\\d+:\\d+)
and
search for SipReg - (.*SipReg)
and
search, tabbed lines, for dnis = 0027
but not sure how to do the last part...
thanks for your help...
sure, my apologies for being vague....
I want my regular expression to:
search for time at the beginning of the line - ^*?(\\@\\d+:\\d+:\\d+)
and
search for SipReg - (.*SipReg)
and
search, tabbed lines, for dnis = 0027
but not sure how to do the last part...
thanks for your help...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The (?m) modifier allows the ^ character to match the start of any line. Without it, it will only match the start of the entire string.
You had ^* which is invalid syntax in a regex. You may have meant to use ^.* ? It's safer to restrict it a bit more than that to prevent unexpected behaviour. I've used [ \\t]* to allow a match of spaces and tabs prior to the @ character, if there are any.
Intead of using .* later in the pattern, I've used [^@]* which:
1. Allows newline characters to be matched too (important, since your data seems to span multiple lines)
2. Won't be too greedy and thus won't start matching data in the following record
You had ^* which is invalid syntax in a regex. You may have meant to use ^.* ? It's safer to restrict it a bit more than that to prevent unexpected behaviour. I've used [ \\t]* to allow a match of spaces and tabs prior to the @ character, if there are any.
Intead of using .* later in the pattern, I've used [^@]* which:
1. Allows newline characters to be matched too (important, since your data seems to span multiple lines)
2. Won't be too greedy and thus won't start matching data in the following record
ASKER
that worked...
thank you so much for your help....
do you mind explaining the regular expression?
thank you so much for your help....
do you mind explaining the regular expression?
Let me know if that isn't enough explanation...
ASKER
thank you TerryAtOpus.. I saw your explanation after I rewarded the points....
your explanation is great!
truly appreciated....
your explanation is great!
truly appreciated....
Open in new window
or something else