0

Is there a way to use a regex expression with wild cards? Specifically, I have a String phrase and another String target. I would like to use the match method to find the first occurrence of the target in the phrase where the character before and after the target is anything other than a-z.

Updated:

Is there a way to use the String method matches() with the following regex:

"(?<![a-z])" + "hello" + "(?![a-z])";
0

1 Answer 1

5

You can use the regex, "(?<![a-z])" + Pattern.quote(phrase) + "(?![a-z])"

Demo at regex101 with phrase = "hello".

  • (?<![a-z]): Negative lookbehind for [a-z]
  • (?![a-z]): Negative lookahead for [a-z]

Java Demo:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {
        // Test
        String phrase = "hello";
        String regex = "(?<![a-z])" + Pattern.quote(phrase) + "(?![a-z])";
        Pattern pattern = Pattern.compile(regex);
        Stream.of(
                "hi hello world",
                "hihelloworld"
        ).forEach(s -> {
            Matcher matcher = pattern.matcher(s);
            System.out.print(s + " => ");
            if(matcher.find()) {
                System.out.println("Match found");
            }else {
                System.out.println("No match found");
            }
        });
    }
}

Output:

hi hello world => Match found
hihelloworld => No match found

In case you want the full-match, use the regex, .*(?<![a-z]) + Pattern.quote(phrase) +(?![a-z]).* as demonstrated at regex101.com. The pattern, .* means any character any number of times. The rest of the patterns are already explained above. The presence of .* before and after the match will ensure covering the whole string.

Java Demo:

import java.util.regex.Pattern;
import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {
        // Test
        String phrase = "hello";
        String regex = ".*(?<![a-z])" + Pattern.quote(phrase) + "(?![a-z]).*";
        Stream.of(
                    "hi hello world", 
                    "hihelloworld"
        ).forEach(s -> System.out.println(s + " => " + (s.matches(regex) ? "Match found" : "No match found")));
    }
}

Output:

hi hello world => Match found
hihelloworld => No match found
Sign up to request clarification or add additional context in comments.

5 Comments

It might be safer to escape the dynamic part. "(?<![a-z])" + Pattern.quote(phrase) + "(?![a-z])"
is there a way to use the String method matches() with "(?<![a-z])" + "hello" + "(?![a-z])";
thank you. could you possibly explain he regex: ".*(?<![a-z])" + Pattern.quote(phrase) + "(?![a-z]).*";
@DCR - The pattern, .* means any character any number of times. The presence of .* before and after the match will ensure covering the whole string. If there is no match, nothing will be covered.
@Arvind Kumar Avinash is the following correct: the .* at the front end captures the entire string up to the target. the .* at the backend captures the string from then end of the target to the end of the phrase. then it essentially performs a nested loop and deletes a character in the front and tries to match , when the entire front piece is deleted it deletes a character from the backend and starts all over on the front end - keeps doing this to see if it can match

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.