Java 使用正则表达式从字符串中提取密码
我正在尝试解决一个练习,其中我必须在给定文本中打印密码。这些规则是: 密码由数字和拉丁字母大写和小写组成; 密码总是跟在“password”后面(可以用大写或小写字母书写),但可以用任意数量的空格和冒号:字符与之分开 我的问题是,我需要确保密码前面有“password”和随机数目的空格和冒号,但我还必须只打印密码 例如,如果输入为:Java 使用正则表达式从字符串中提取密码,java,regex,string,Java,Regex,String,我正在尝试解决一个练习,其中我必须在给定文本中打印密码。这些规则是: 密码由数字和拉丁字母大写和小写组成; 密码总是跟在“password”后面(可以用大写或小写字母书写),但可以用任意数量的空格和冒号:字符与之分开 我的问题是,我需要确保密码前面有“password”和随机数目的空格和冒号,但我还必须只打印密码 例如,如果输入为: My email javacoder@gmail.com with password SECRET115. Here is my old PASSWORD:
My email javacoder@gmail.com with password SECRET115. Here is my old PASSWORD: PASS111.
输出应为:
SECRET115
PASS111
SECRET115
PASS111
我偶然发现了lookaheads和lookbehinds,并在我的正则表达式中尝试了它们:
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
Pattern pattern = Pattern.compile("(?<=password[\\s:]*)\\w*(?=\\W)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
if (!matcher.find()) {
System.out.println("No passwords found.");
}
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
另外,当我尝试将正则表达式更改为
”(?时,由于if条件中的matcher.find()调用,第一个匹配被吃掉。仅从第二个匹配开始生成输出。您可以使用正则表达式,(?try(?I)(密码[\s:*)(\w+
并在每场比赛中抽取第二组
在java中,变量长度查找是不可能的…当您的密码正则表达式包含\w*
时,正则表达式首先匹配一个空字符串。由于您在if
条件下调用matcher.find()
。当您使用\w{5}
,第一个匹配是SECRET115
,它不会显示
使用
看
解释
NODE EXPLANATION
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
password 'password'
--------------------------------------------------------------------------------
[\s:]* any character of: whitespace (\n, \r, \t,
\f, and " "), ':' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
:
输出:
private静态最终模式密码\u模式=
Pattern.compile(“密码\\s*:?\\s*(?[A-Za-z0-9]+)”,Pattern.CASE不区分大小写);
公共静态列表getAllPasswords(字符串str){
Matcher Matcher=密码模式Matcher(str);
列出密码=新建ArrayList();
while(matcher.find()){
密码。添加(matcher.group(“密码”);
}
返回密码;
}
演示您可以在中找到,根据要求,密码由数字和拉丁大写字母和小写字母组成。您已经使用了w+
,除了字母和数字之外,它还选择下划线()。您应该使用\p{Alnum}+
或[a-Za-z0-9]+
@ArvindKumarAvinash已修复。这是详细信息,但仍然存在。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(?<=password|password:)\\s*(\\p{Alnum}+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern
.matcher("My email javacoder@gmail.com with password SECRET115. Here is my old PASSWORD: PASS111.");
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
SECRET115
PASS111
\bpassword[\s:]*(\w+)
NODE EXPLANATION
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
password 'password'
--------------------------------------------------------------------------------
[\s:]* any character of: whitespace (\n, \r, \t,
\f, and " "), ':' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
Pattern pattern = Pattern.compile("\\bpassword[\\s:]*(\\w+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
Boolean found = false;
while (matcher.find()) {
System.out.println(matcher.group(1));
found = true;
}
if (!found) {
System.out.println("No passwords found.");
}
SECRET115
PASS111
private static final Pattern PASSWORD_PATTERN =
Pattern.compile("password\\s*:?\\s*(?<password>[A-Za-z0-9]+)", Pattern.CASE_INSENSITIVE);
public static List<String> getAllPasswords(String str) {
Matcher matcher = PASSWORD_PATTERN.matcher(str);
List<String> passwords = new ArrayList<>();
while (matcher.find()) {
passwords.add(matcher.group("password"));
}
return passwords;
}