简体   繁体   English

此字符串的正则表达式模式

[英]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 "(" ")" 我所知道的是,并非总是有单词“ java”,因此我想在最后一个“(””)之间取值

Thank you all for any clues 谢谢大家的线索

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. 然后使用match.Groups [1] .Value获取内部组的值。

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. 更新:@cabecao提醒我,如果文本始终显示在末尾,并且可能还有其他括号,则在末尾添加“ $”以匹配行尾。

\([^)]+\)$

You may use: 您可以使用:

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

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 . 它将匹配一组非括号字符,在字符串末尾先跟一个,再跟一个。

我现在想到的最简单的方法是:

.*\((.*)\)$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM