String myString = "abc.kiransinh.bapu.abc.events.KiranSinhBorasibBapu";
Pattern regex = Pattern.compile("[^.]+(?=[^.]*$)[^Bapu]");
System.out.println(myString);
Matcher regexMatcher = regex.matcher(myString);
if (regexMatcher.find()) {
String ResultString = regexMatcher.group();
ResultString=ResultString.replaceAll("(.)(\\p{Lu})", "$1_$2").toUpperCase();
System.out.println(ResultString);
}
欲望输出为:KIRAN_SINH_BORASIAB
我试过上面的代码。想要只使用正则表达式。 虽然我使用了replaceAll方法。
只使用Regex可能会产生所需的输出。 我是regex的新手。任何帮助都会受到太多的赞赏。 在此先感谢: - )
答案 0 :(得分:1)
您可以使用此正则表达式匹配您想要的字符串:
String re = "(?<=\\.)(?=[^.]*$)\\p{Lu}\\p{L}*?(?=\\p{Lu}(?=[^\\p{Lu}]*$))";
Pattern pattern = Pattern.compile(re);
使用Matcher#find
获取匹配的文字。
Matcher regexMatcher = pattern.matcher( input );
if (regexMatcher.find()) {
System.out.println( regexMatcher.group() );
}
RegEx分手:
(?<=\.) # lookbehind to assert preceding char is DOT
(?=[^.]*$) # lookahead to assert there is no further DOT in text
\p{Lu} # match a unicode uppercase letter
\p{L}*? # match 0 or more of unicode letters (non-greedy)
(?=\p{Lu}(?=[^\p{Lu}]*$)) # make sure next char is uppercase letter and further
# (?=[^\p{Lu}]*$) makes sure there is no uppercase letter after