[英]Need help in extracting a expression from a log using regular expressions in Java
I have a log as a string, and I am trying to capture the error message from it. 我有一个日志作为字符串,并且我试图从中捕获错误消息。 the regex I tried did not work. 我尝试过的正则表达式不起作用。
String = "Retrying for error: [[\\"billing\\",\\{u'non_field_errors': [u'Invalid payment email provided']}\\"]]" String =“重试错误:[[\\”帐单\\“,\\ {u'non_field_errors':[u'提供了无效的付款电子邮件']} \\”]]“
I need to extract the error message which is 我需要提取错误消息是
Invalid payment email provided 提供的付款电子邮件无效
How I can extract this, using regex? 我如何使用正则表达式提取它?
I tried the pattern Retrying for error: (\\\\.+)
, but it doesn't work: 我尝试了模式Retrying for error: (\\\\.+)
,但是不起作用:
String pattern = "Retrying for error: (\\.+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(unescapedStr);
if (m.find()) {
error = m.group(1);
}
How can I get the expected result? 如何获得预期的结果?
Invalid payment email provided, Actual: null 提供了无效的付款电子邮件,实际:空
you dont have to use regex. 您不必使用正则表达式。 how about this 这个怎么样
String x = "Retrying for error: [[\"billing\",\\{u'non_field_errors': [u'Invalid payment email provided']}\"]]";
String c = x.replace("Retrying for error: ","");
String g = c.substring(c.lastIndexOf('[')+1);
String v = g.substring(0, g.indexOf(']'));
System.out.println(v);
this prints 此打印
u'Invalid payment email provided'
now do your logs have multiple instances of Retrying for error:
? 现在您的日志中是否有多个Retrying for error:
实例Retrying for error:
also, does this 还可以吗
"Retrying for error: [[\\"billing\\",\\\\{u'non_field_errors': [u'Invalid payment email provided']}\\"]]";
represent a single line in your logs? 代表您的日志中的一行?
The main idea is this: If each line in your log file has just one instance of Retrying for error:
, then you can easily parse the log one line at a time and iteratively strip away stuff that you dont need. 主要思想是:如果日志文件中的每一行只有一个Retrying for error:
实例,那么您可以轻松地一次解析该日志一行,并反复剥离不需要的内容。
You may use the following regex: 您可以使用以下正则表达式:
Retrying for error:.*\[u'([^']+)
See the regex demo . 参见regex演示 。
Details 细节
Retrying for error:
- a literal substring Retrying for error:
-文字子字符串 .*
- any 0+ chars other than line break chars, as many as possible .*
-尽可能多的除换行符以外的0+个字符 \\[u'
- a [u'
substring \\[u'
一个[u'
子字符串 ([^']+)
- Capturing group #1 ( matcher.group(1)
value): 1+ chars other than '
. ([^']+)
-捕获组#1( matcher.group(1)
值):除'
以外'
1个字符。 See the Java demo : 参见Java演示 :
String unescapedStr = "Retrying for error: [[\"billing\",\\{u'non_field_errors': [u'Invalid payment email provided']}\"]]";
String pattern = "Retrying for error:.*\\[u'([^']+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(unescapedStr);
if (m.find()) {
System.out.println(m.group(1));
}
// => Invalid payment email provided
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.