1

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.

2
  • 1
    Why did you put the letters in []? Don't. --- Do you really want the letters to be optional, so it matches e.g. the word ho? Change * to + to require at least one of each letter. --- Replace both \\s with \\b to match word boundaries. --- Regex should be "\\bh+e+l+l+o+\\b". See regex101 for demo. Commented Feb 11, 2017 at 4:56
  • Thanks, it should def be + Commented Feb 11, 2017 at 5:40

3 Answers 3

1

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

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

Comments

1

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.

Comments

1

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));

3 Comments

I can already get that output. I'm trying to input Heeeellllooo and return Hello
use ReplaceAll: System.out.println(line.replaceAll(makeRegex(input), input));
Why {1,} instead of + in your regex maker? Also, what about words like Hello where there are two 'l's? Would the first l in the regex eat all the ls?

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.