gpt4 book ai didi

java - 从字符串中获取用户名 = 'testuserMM' 的正则表达式

转载 作者:行者123 更新时间:2023-11-30 11:01:04 24 4
gpt4 key购买 nike

我试过这个正则表达式来捕获用户名

highs\(\d+\)\[.*?\]\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]\sftid\(\d+\):\s.

它没有用。

<55>Mar 17 12:02:00 forcesss-off [Father][1x91422234][eee][hote] abcd(QlidcxpOulqsf): highs(23455814)[mothers][192.192.21.12] ftid(64322816): oops authentication failed with (http-commo-auth, username='testuserMM' password='********'congratulation-fakem='login' )

最佳答案

您可以为此使用更简单的正则表达式:

\busername='([^']+)

参见 demo ,结果在第 1 组。

正则表达式:

  • \b - 单词边界
  • username=' - 文字字符串 username='
  • ([^']+) - 包含我们的子字符串的捕获组,该子字符串仅包含 1 个或多个符号,而不是单个撇号。

更新:

这里有两种获取您要查找的文本的方法:

String str = "<55>Mar 17 12:02:00 forcesss-off [Father][1x91422234][eee][hote] abcd(QlidcxpOulqsf): highs(23455814)[mothers][192.192.21.12] ftid(64322816): oops authentication failed with (http-commo-auth, username='testuserMM' password='********'congratulation-fakem='login' )";
String res = str.replaceAll(".*\\busername='([^']+)'.*", "$1");
System.out.println(res);

String rx = "(?<=\\busername=')[^']+";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
System.out.println(m.group());
}

参见 IDEONE demo

关于java - 从字符串中获取用户名 = 'testuserMM' 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31344016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com