Phani Kumar Bhamidipati
Phani Kumar Bhamidipati

Reputation: 13283

How to find a string including non word character?

How to find a string including non word character?

Example input: l l'oreal s.a. l l' ab l
search String: l
output: XX l'oreal s.a. XX l' ab l
Expected: XX l'oreal s.a. XX l' ab X

I was trying to find the search string in the input string using the below regex.

    String inputStr = "l l'oreal s.a. l l' ab l";
    System.out.println(inputStr);
    String searchStr = "l";

    Pattern pattern = Pattern.compile("(\\b"+ searchStr +"\\b)(?=[\\s])");
    Matcher matcher = pattern.matcher(inputStr);

    if ( matcher.find()){
        String rep = matcher.replaceAll("XX");
        System.out.println("REP:" + rep);
    }else{
        System.out.println("no match...");
    }

The regex pattern searches for the string where it is followed by a space(\s). But in the above example it doesn't work for the last character since it was not followed by a space.

The main goal was to find string with non word characters like... private-limited ( when searching for private should return false) Hello! ( false when searched for Hello) Couple of patterns which tried but not working...

pattern = Pattern.compile("(?<![\\w+])" + searchStr + "(?![\\W+])", Pattern.CASE_INSENSITIVE);
pattern = Pattern.compile("(?<=[\\s])(\\b"+ searchStr +"\\b)(?=\\s)");

In the above example if I replace searchString = "l'" it doesn't match anything.

Is my approach correct? What is that I am missing?

Thanks.

Upvotes: 0

Views: 234

Answers (2)

Rohit Jain
Rohit Jain

Reputation: 213233

Seems like you don't really need word boundaries there. You want matches, which are surrounded by spaces or end of strings. For that, you can use:

Pattern pattern = Pattern.compile("((?<=\\s|^)"+ searchStr +"(?=\\s|$))");

Upvotes: 1

anubhava
anubhava

Reputation: 785108

Change your lookahead to:

(?=\\s|$)

This will make sure space or end of input is followed oyur match.

Upvotes: 0

Related Questions