英文:
finding out if the characters of a string exist in another string with the same order or not using regex in java
问题
我想用正则表达式在Java中编写一个程序,从输入中获取两个字符串(第一个字符串比第二个字符串短),然后如果第一个字符串的字符在第二个字符串中按相同的顺序但它们不需要彼此相邻(这不是子字符串),则输出"true",如果不是,则输出"false"。以下是一个示例:
示例1:
输入:
phantom
pphvnbajknzxcvbnatopopoim
输出:
true
在上面的示例中,我们可以明显看到单词"phantom"在第二个字符串中(字符按相同顺序排列)。
示例2:
输入:
apple
fgayiypvbnltsrgte
输出:
false
如您所见,苹果不符合我之前提到的条件存在于第二个字符串中,因此输出为false。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String word1 = input.next();
String word2 = input.next();
String pattern = "";
int n = word1.length();
char[] word1CharArr = word1.toCharArray();
for (int i = 0; i < n; i++) {
pattern += ".*\\b" + word1CharArr[i] + "\\b.*";
}
pattern = "^" + pattern + "$";
System.out.println(word2.matches(pattern));
}
}
这是我做的。我将第一个字符串拆分为其字符,并希望在每个字符之前和之后使用正则表达式来确定模式。我对正则表达式进行了大量搜索,但仍然在这里遇到问题。我已经注释掉的部分来自我的一次搜索,但它没有起作用。
我强调我想使用正则表达式解决它,而不是其他方式。
英文:
i want to write a program in java using REGEX that gets 2 strings from the input ( the first one is shorter than the second one ) and then if the characters of the first string was inside the second string with the same order but they do not need to be next to each other ( it is not substring ) it outputs "true" and if not it outputs "false" here's an example:
example1:
input:
phantom
pphvnbajknzxcvbnatopopoim
output:
true
in the above example it is obvious we can see the word "phantom" in the second string (the characters are in the same order)
example2:
input:
apple
fgayiypvbnltsrgte
output:
false
as you can see apple dos not exists in the second string with the conditions i have earlier mentioned so it outputs false
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String word1 = input.next();
String word2 = input.next();
String pattern = "";
int n = word1.length();
char[] word1CharArr = word1.toCharArray();
for ( int i = 0 ; i < n ; i++) {
pattern += "[:alnum:]" +word1CharArr[i]+"[:alnum:]";
// pattern += ".*\\b|\\B" +word1CharArr[i]+"\\b|\\B";
}
pattern = "^" + pattern + "$";
// pattern = "(?s)" + pattern + ".*";
// System.out.println(pattern);
System.out.println(word2.matches(pattern));
}
}
here is what i did . i broke my first string to its characters and want to use REGEX before and after each character to determine the pattern. I have searched much about REGEX and how to use it but still i have problem here. the part i have commented comes out from one of my searches but it did not work
I emphasize that i want to solve it with REGEX not any other way.
答案1
得分: 1
[:alnum:]
不是一个有效的东西。即使它是,它只会匹配恰好一个字符,而不是“任意数量,从0到无限多个”。
你只需要在中间使用 .*
的方式来匹配 phantom
:^.*p.*h.*a.*n.*t.*o.*m.*$
就足够了。毕竟,phantom
匹配,paahaanaataaoaamaa 也匹配 -
String pattern = word1.chars()
.mapToObj(c -> ".*" + (char) c)
.collect(Collectors.joining()) + ".*";
应该能完成任务。
英文:
[:alnum:]
isn't a thing. Even if it is, that would match exactly one character, not 'any number, from 0 to infinitely many of them'.
You just want phantom
with .*
in the middle: ^.*p.*h.*a.*n.*t.*o.*m.*$' is all you need. After all,
phantom` 'fits', and so does paahaanaataaoaamaa -
String pattern = word1.chars()
.mapToObj(c -> ".*" + (char) c)
.collect(Collectors.joining()) + ".*";
should get the job done.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论