user1750355
user1750355

Reputation: 326

Regex pattern for this string

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

Upvotes: 0

Views: 116

Answers (4)

Bergi
Bergi

Reputation: 665564

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.

Upvotes: 2

Alex Filipovici
Alex Filipovici

Reputation: 32581

You may use:

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

Upvotes: 2

Adam Adamaszek
Adam Adamaszek

Reputation: 4054

The simplest I can think of now:

.*\((.*)\)$

Upvotes: 1

samjudson
samjudson

Reputation: 56903

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.

\([^)]+\)$

Upvotes: 2

Related Questions