0

I'm having a problem with a regex that should simply replace one string with another. Basically, I want to convert any caps variant of LLC and replace it with LLC. Eg. Llc would become LLC. However, in my case, the result is just the replacement string. There's something that should be obvious that I'm missing.

String pattern = "(?i)(.*?)\\b(LLC.?)\\b(.*?)";
String replacement = "LLC";
String unformatted = "Midwest Horticultural Llc";

String formatted = unformatted.replaceAll(pattern, replacement);

My expectation is that formatted string will be:

Midwest Horticultural LLC

But what I end up with is actually:

LLC

If someone could show me the error of my ways, I would appreciate it.

3
  • Why is that your expectation? What does your pattern match? Commented Nov 19, 2015 at 21:00
  • 2
    Why do you have .*? before your LLC? Commented Nov 19, 2015 at 21:01
  • If I don't use the .?*, the regex does not match the LLC substring. If LLC is the only word in the string, it matches. Commented Nov 19, 2015 at 21:04

3 Answers 3

1

The problem is because you have (.*?) before and after (LLC.?) in your pattern. That means that it will replace everything (since you match any number of any character) with your new string, not just the one section you wanted. If you remove that, it works fine.

String pattern = "(?i)\\bLLC\\b";
String replacement = "LLC";
String unformatted = "Midwest Horticultural Llc";

String formatted = unformatted.replaceAll(pattern, replacement);
System.out.println(formatted);

Gives you "Midwest Horticultural LLC"

Remember, you don't need your regex to match the entire String when doing a replacement. It's only looking for substrings which match your regex.

Sign up to request clarification or add additional context in comments.

2 Comments

That was it. Thanks. I'll mark this as the answer when the system lets me.
@DavidNedrow: I think the system already lets you accept the answer :)
1

Leading and trailing .* will cause a match on the whole string if it contains 'LLC'. I believe you are looking for this regex:

String pattern = "(?i)\\bLLC\\b\\.?"; // Note the backslash before the dot
String replacement = "LLC";
String unformatted = "Midwest Horticultural Llc";

String formatted = unformatted.replaceAll(pattern, replacement);

System.out.println(formatted); // Prints "Midwest Horticultural LLC"

Comments

0

Does this satisfy all your use-cases?

private static void replaceAll() {
        String regex = "(?i)\\b(LLC)\\b";
        String unformatted = "Midwest Horticultural Llc";
        String replacement = "LLC";

        String formatted = Pattern.compile(regex).matcher(unformatted).replaceAll(replacement);

        System.out.println(formatted);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.