0

I have a problem to match some string in fallowing lines:

02-16 12:23:24.046  3068  3068 E AndroidRuntime:    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153) 
02-16 12:23:24.046  3068  3068 E AndroidRuntime:    at android.view.Window$LocalWindowManager.addView(Window.java:559)
02-16 12:23:24.046  3068  3068 E AndroidRuntime:    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2716)
02-16 12:23:24.046  3068  3068 E AndroidRuntime:    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2151)
02-16 12:23:24.046  3068  3068 E AndroidRuntime:    at android.app.ActivityThread.access$700(ActivityThread.java:140)
02-16 12:23:24.046  3068  3068 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
02-16 12:23:24.046  3068  3068 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:99)

And i want to match string between two last '(' ')' chars. I know there is a lot of regex tutorials, but i just cannot get it yet. I need to study more about this

So result of success should be:

WindowManagerImpl.java:153
Window.java:559
ActivityThread.java:2716
ActivityThread.java:2151
and so on

All i know is that there is not always word "java", so I want to take value between last "(" ")"

Thank you all for any clues

4
  • Why you need regex? Logcat is TAB delimited IIRC. Commented Feb 18, 2013 at 10:13
  • What does it have to do with design patterns ? Commented Feb 18, 2013 at 10:13
  • 2
    What have you tried? Commented Feb 18, 2013 at 10:15
  • You could also simply use Substring and LastIndexOf. Commented Feb 18, 2013 at 10:18

4 Answers 4

2

Assuming there are no other parenthesis in the line then something like this would work:

\([^)]+\)

You could then trim the ( and ) off the ends of the match.

Or group in inner text:

\(([^)]+)\)

And then use match.Groups[1].Value to get value of the inner group.

Update: And @cabecao reminded me, if the text always appears at the end, and there might be other parenthesis then add a "$" at the end to match the end of a line.

\([^)]+\)$
Sign up to request clarification or add additional context in comments.

Comments

2

You may use:

var matches = Regex.Matches(input, @"(?<=\().*(?=\))");
foreach (Match item in matches)
{
    Console.WriteLine(item.Value);
}

Comments

2

This should do it:

(?<=\()[^)]+(?=\)$)

Using lookarounds instead of capturing groups to make extracting the result easier. It will match a group of non-parenthesis characters preceded by one and followed by one at the end of the string.

Comments

1

The simplest I can think of now:

.*\((.*)\)$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.