Yawn
Yawn

Reputation: 173

Java, can regex's take variables?

I was looking at REGEX's as a possible solution for a problem. I made some sample Strings that contained words such as hello, hellllooo, hhhheello, etc. I then created a regex to find all of these types of words. What I not what to do is look at sentence that may or may not contain an inputted word. For example, you type in hellloo, I would like to scan my sentence for words similar to "hellloo" and return 'hello' if found in the sentence. Can I create a regex that is sort of a variable of the user input? If you type in hellloo then I would construct something that returns back similar words to your input from a sentence or file.

Two input strings

String line = "this is hello helloooo hellllooo hhhel hellll what can I do?";
String longLine = "hello man this what can up down where hey my there find now ok stuff jive super sam dude car";

my regex function

public static void regexChecker(String theRegex, String str2Check) {
        Pattern checkRegex = Pattern.compile(theRegex);
        Matcher regexMatcher = checkRegex.matcher(str2Check);

        while(regexMatcher.find()) {
            if(regexMatcher.group().length() != 0) {
                System.out.println(regexMatcher.group().trim());
            }
        }
    }

running this

regexChecker("\\s[h]*[e]*[l]*[l]*[o]*\\s", line);

returns

hello
hello
hellllooo
hellll

I would like to create a REGEX based off the user input 'helllooo' that returns hello from the second String longLine. Not sure if regex is the right solution, but I would like to know if its possible.

Upvotes: 1

Views: 114

Answers (3)

Islam Shafie
Islam Shafie

Reputation: 21

try this

make regex function:

public static String makeRegex(String input) {
StringBuilder regex = new StringBuilder();
if(input != null && input.length() > 0) {

    for (int i = 0; i < input.length(); i++) {
        regex.append("[" + input.charAt(i) + "]{1,}");

    }
}
return regex.toString();
}

regex function:

public static ArrayList regexChecker(String theRegex, String str2Check) {
        Pattern checkRegex = Pattern.compile(theRegex);
        Matcher regexMatcher = checkRegex.matcher(str2Check);
       ArrayList<String> list = new ArrayList<>();
       String findString = "";
        while(regexMatcher.find()) {
            if(regexMatcher.group().length() != 0) {
                findString = regexMatcher.group().trim();
                list.add(findString);

            }
        }
        return list;
    }

used:

String line = "this is hello helloooo hellllooo hhhel hellll what can I do?";

    String input = "hello";

    ArrayList<String> list = regexChecker(makeRegex(input), line);

    for(int i = 0;i<list.size();i++)
        System.out.println(list.get(i));

returns:

hello
helloooo
hellllooo

Replace String:

System.out.println(line.replaceAll(makeRegex(input), input));

Upvotes: 1

Bohemian
Bohemian

Reputation: 424983

Try this:

String found = input.replaceAll(".*?((h)+(e)+(l)(l)+(o)+)?.*", "$2$3$4$5$6");

The result will either be "hello" or blank if there's nothing like hello in the input.

Upvotes: 1

David Winton
David Winton

Reputation: 86

Assuming your example captures what you want to do (repeated letters from the input word), you definitely can.

Conceptually, you want the following:

public String makeRegec(String input) {
    StringBuilder regex = new StringBuilder("\\b");
    if(input != null && input.length() > 0) {

        for (int i = 0; i < input.length(); i++) {
            regex.append(input.charAt(i)).append("+");
        }
    }
    regex.append("\\b*");//don't need this if you would accept hello, for example
    return regex.toString();
}

Of course, you could compile the pattern and return that instead

Upvotes: 1

Related Questions