如何在java中使用正则表达式和拆分? [英] How do I use regex and split in java?
问题描述
我有c#代码
var matches = Regex.Split(sentence.ToLower( ), \\b)。相交(关键字)
string matchWord = 字符串 .Join( ,匹配);
int countWord = Regex.Matches(matchWord, @ [A-Za-z0-9] +)。计数;
此代码将匹配匹配字并计算它。
如何将其转换为java?
i希望匹配句子与关键字,输出将是匹配的关键字。
例如:
关键字:买,想要,卖出
输入:我想买一些苹果。
输出:买想要
输出2:2
< b>我尝试了什么:
List< String> keyword = Arrays.asList( word1, word2, word3);
扫描仪输入= 新扫描仪(System.in);
String sentence = in.nextLine();
字符串 patternString = \\b( + StringUtils.join(keyword, |) + )\\b;
模式模式= Pattern.compile(patternString);
Matcher matcher = pattern.matcher(句子);
.join部分是错误,我不知道为什么。当我将鼠标悬停在它上面时,它说找不到符号。
i不知道如何将.count转换为java,因为我所知道的正则表达式是boolean String.Matches()。
请参阅课程:正则表达式(Java教程) [ ^ ]。
String strArray [] = sentence.split(\\ b)
String result = String.join(...,strArray);
System.out.println(结果);
是不是一样?
http://www.dotnetperls.com/join-java [ ^ ]
I have c# code which is
var matches = Regex.Split(sentence.ToLower(), "\\b").Intersect(keyword)
string matchWord= String.Join(" ", matches);
int countWord = Regex.Matches(matchWord, @"[A-Za-z0-9]+").Count;
this code will match the matches word and count it.
How do I convert it to java?
i want to match sentence with keyword and the output will be the keyword that matches.
example :
keyword : "buy", "want", "sell"
input : I want to buy some apple.
output : buy want
output2 : 2
What I have tried:
List<String> keyword = Arrays.asList("word1", "word2", "word3");
Scanner in = new Scanner(System.in);
String sentence = in.nextLine();
String patternString = "\\b(" + StringUtils.join(keyword, "|") + ")\\b";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(sentence);
the .join part is error and i don't know why. It said "cannot find symbol" when I hover over it.
i have no idea how to convert .count into java, because all I know for regex fuction is boolean String.Matches().
See Lesson: Regular Expressions (The Java Tutorials)[^].
String strArray[] = sentence.split("\\b")
String result = String.join("...", strArray);
System.out.println(result);
is it not the same ??
http://www.dotnetperls.com/join-java[^]
这篇关于如何在java中使用正则表达式和拆分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!