Reputation: 459
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);
}
Desire Output is: KIRAN_SINH_BORASIAB
i tried Above code.Want to use only Regex. though i have used replaceAll method.
Desired Output might be possible using only Regex. I am new to regex.Any help woud be too much appreciated. Thanks in Advance :-)
Upvotes: 2
Views: 623
Reputation: 785246
You can use this regex to match the string you desire:
String re = "(?<=\\.)(?=[^.]*$)\\p{Lu}\\p{L}*?(?=\\p{Lu}(?=[^\\p{Lu}]*$))";
Pattern pattern = Pattern.compile(re);
Use Matcher#find
to get your matched text.
Matcher regexMatcher = pattern.matcher( input );
if (regexMatcher.find()) {
System.out.println( regexMatcher.group() );
}
RegEx Breakup:
(?<=\.) # 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
Upvotes: 1