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

Posted on 2012-09-06
Last Modified: 2012-09-06
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...
Question by:epifanio67
    LVL 34

    Expert Comment

    by:Terry Woods
    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

    Author Comment

    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...
    LVL 34

    Accepted Solution

    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?)
    LVL 34

    Expert Comment

    by:Terry Woods
    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

    Author Closing Comment

    that worked...
    thank you so much for your help....

    do you mind explaining the regular expression?
    LVL 34

    Expert Comment

    by:Terry Woods
    Let me know if that isn't enough explanation...

    Author Comment

    thank you TerryAtOpus.. I saw your explanation after I rewarded the points....

    your explanation is great!

    truly appreciated....

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now