[英]Java RegExp can't get the result ater evaluating pattern
Hi I have been trying to learn RegExpresions using Java I am still at the begining and I wanted to start a little program that is given a string and outputs the syllabels split.This is what I got so far: 嗨,我一直在尝试使用Java学习RegExpresions我还在开始,我想开始一个给出字符串的小程序并输出syllabels split.This是我到目前为止:
String mama = "mama";
Pattern vcv = Pattern.compile("([aeiou][bcdfghjklmnpqrstvwxyz][aeiou])");
Matcher matcher = vcv.matcher(mama);
if(matcher){
// the result of this should be ma - ma
}
What I am trying to do is create a pattern that checks the letters of the given word and if it finds a pattern that contains a vocale/consonant/vocale it will add a "-" like this v-cv .How can I achive this. 我要做的是创建一个模式来检查给定单词的字母,如果它找到一个包含vocale / consonant / vocale的模式,它将添加一个“ - ”,就像这个v-cv。我怎么能得到这个。
In the following example i matched the first vowel and used positive lookahead for the next consonant-vowel group. 在下面的例子中,我匹配了第一个元音,并为下一个辅音 - 元音组使用了正向前瞻。 This is so i can split again if i have a vcvcv group.
如果我有一个vcvcv组,我就可以再次拆分。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
new Test().run();
}
private void run() {
String mama = "mama";
Pattern vcv =
Pattern.compile("([aeiou])(?=[bcdfghjklmnpqrstvwxyz][aeiou])");
Matcher matcher = vcv.matcher(mama);
System.out.println(matcher.replaceAll("$1-"));
String mamama = "mamama";
matcher = vcv.matcher(mamama);
System.out.println(matcher.replaceAll("$1-"));
}
}
Output: 输出:
ma-ma ma-ma-ma
try 尝试
mama.replaceAll('([aeiou])([....][aeiou])', '\1-\2');
replaceAll is a regular expression method replaceAll是一个正则表达式方法
Your pattern only matches if the String starts with a vocal. 如果String以人声开头,则您的模式仅匹配。 If you want to find a substring, ignoring the beginning, use
如果要查找子字符串,忽略开头,请使用
Pattern vcv = Pattern.compile (".*([aeiou][bcdfghjklmnpqrstvwxyz][aeiou])");
If you like to ignore the end too: 如果你也想忽略这个结局:
Pattern vcv = Pattern.compile (".*([aeiou][bcdfghjklmnpqrstvwxyz][aeiou]).*");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.