codingbat wordEnds using regex
Posted
by polygenelubricants
on Stack Overflow
See other posts from Stack Overflow
or by polygenelubricants
Published on 2010-04-02T11:54:42Z
Indexed on
2010/04/02
12:03 UTC
Read the original article
Hit count: 675
I'm trying to solve wordEnds
from codingbat.com using regex. This is the simplest as I can make it with my current knowledge of regex:
public String wordEnds(String str, String word) {
return str.replaceAll(
String.format(
".*?(?=%s)(?<=(.|^))%1$s(?=(.|$))|.+",
java.util.regex.Pattern.quote(word)
),
"$1$2"
);
}
String.format
is used to inject word
into the pattern for both readability and convenience (it's injected twice). Pattern.quote
isn't necessary to pass their tests, but I think it's required for a proper regex-based solution.
The regex has two major parts:
- If after matching as few characters as possible "
.*?
",word
can still be found "(?=%s)
", then lookbehind to capture any character immediately preceding it "(?<=(.|^))
", matchword
"%1$s
" and lookforward to capture any character following it "(?=(.|$))
".- The initial "if" test ensures that the atomic lookbehind captures only if there's a
word
- Using lookahead to capture the following character doesn't consume it, so it can be used as part of further matching
- The initial "if" test ensures that the atomic lookbehind captures only if there's a
- Otherwise match what's left "
|.+
"- Groups 1 and 2 would capture empty strings
I think this works in all cases, but it's obviously quite complex. I'm just wondering if others can suggest a simpler regex to do this.
Note: I'm not looking for a solution using indexOf
and a loop. I want a regex-based replaceAll
solution. I also need a working solution that I can just copy-paste into codingbat and passes.
© Stack Overflow or respective owner