2016-12-30 68 views
-2

我有一个模式在日志如下:正则表达式匹配否定

| App: | -> which doesn't have the app information 
| App: Registration | -> which indicates that this is registration app. 

我希望能够找到从日志中| App: |的。

我试过下面的正则表达式但失败了,所以决定在这里问。

/App: ^(.*) |?/g和其他一些但失败。

帮助表示赞赏。

+1

你试过'/ | App:|/g'?或'/ |应用程序:* |/g'在多个空格的情况下。 – Andreas

+0

你想在'Java'或'C#'中使用这个正则表达式吗 –

回答

1

你必须逃避Java中的竖线。在它自己的垂直条上是一个正则表达式中的运算符。 https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

String test = "| App: | ABC"; 
String test2 = "| App: Registration |"; 
String match = "\\| App:.*\\|.*"; 
System.out.println(test.matches(match)); 
System.out.println(test2.matches(match)); 
+0

我试过了“App:\ |”但没有工作。 – DarthVader

+0

Wellll ...你也必须逃避反斜线:)哈哈,它就像一个非常糟糕的恶作剧,Java的人正在玩你!请参阅上面的ProgrammersBlock代码示例。 – Barett