Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Java: regular expression: how do I search in multiple tabbed lines?

Hello Experts,

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) 	

Open in new window

In advance, thank you for your help...
  • 4
  • 3
1 Solution
Terry WoodsIT GuruCommented:
Can you please clarify what the desired output is? I'm not clear whether you want:
	DNIS	'0027'

Open in new window

or something else
epifanio67Author Commented:
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+)
search for SipReg - (.*SipReg)
search, tabbed lines, for dnis = 0027

but not sure how to do the last part...

thanks for your help...
Terry WoodsIT GuruCommented:
If there are newline (\n) characters between the fields you want, then it's not really all on one "line" from a regex perspective.

Try this:
		Pattern p = Pattern.compile("(?m)^[ \\t]*(@\\d+:\\d+:\\d+)([^@]*SipReg)[^@]*"+dnis);

Open in new window

I'm assuming that the @ character will either be at the start of the line, or that there will only be spaces or tabs between it and the start of the line.

Currently you've got capturing groups around the time and the text leading to SipReg. Did you want to capture anything else? (Did you even need to capture that much?)

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Terry WoodsIT GuruCommented:
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
epifanio67Author Commented:
that worked...
thank you so much for your help....

do you mind explaining the regular expression?
Terry WoodsIT GuruCommented:
Let me know if that isn't enough explanation...
epifanio67Author Commented:
thank you TerryAtOpus.. I saw your explanation after I rewarded the points....

your explanation is great!

truly appreciated....

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now