1

How would you set up the expression to find that the keywords do not exist in input string? Key words are: eggs, bacon, cheese

Function is:

    if (!line.matches("eggs") || !line.contains("bacon")|| !line.contains("cheese") )
    {
        match=line;
    }

    return matchregex;
MadeleineG
  • 31
  • 7
  • 1
    what have you tried so far? – Scary Wombat Dec 28 '18 at 07:48
  • I have tried: 1. if (!line.contains("eggs") || !line.contains("bacon")|| !line.contains("cheese") ) //2. if (line.toLowerCase().matches("(.*?cheese:.*)|(.*?eggs:.*) //3. Ive tried to find examples for the past two days. //4. I noticed that if (!line.contains("eggs") ) works, but if I try to add anymore words it will not work at all and ignore my keywords completely. – MadeleineG Dec 28 '18 at 07:59
  • Look at this [example](https://stackoverflow.com/questions/15130309/how-to-use-regex-in-string-contains-method-in-java) and put the code into a method, use that method to return the opposite of its result – Scary Wombat Dec 28 '18 at 08:04
  • As a side note: just for checking those three words I would not recommend using a regex. The usual string methods for finding text are faster in that particular case. – Sascha Dec 28 '18 at 08:09
  • @Sascha Interesting, do you believe that raw performance is a critical factor in this project? :-) – ᴇʟᴇvᴀтᴇ Dec 28 '18 at 08:21
  • raw performance is not a factor at this time. I am just wanting to learn how to filter a list out using different methods. I ended up using if (!line.matches(".*\\b(egg|bacon)\\b.*")). I am now trying to see if two variables can be checked with the "contains". Like variable1 is similiar to variable 2 – MadeleineG Dec 28 '18 at 08:46

2 Answers2

4

A regular expression for matching eggs or bacon or cheese is:

(eggs|bacon|cheese)

You need to detect when lines don't match, so you can negate the condition something like this:

if (!line.matches("(eggs|bacon|cheese)") {
    // Do something
}

This matches the entire line. If you want to tell whether the line contains any of those words, you would need to match differently—either using .* to match the rest of the line, or using Pattern and Matcher.

I would use: \b to mark word boundaries, to ensure you only match cheese and don't match cheesecake.

Pattern pattern = Pattern.compile("\\b(eggs|bacon|cheese)\\b");
Matcher matcher = pattern.matcher(line);
if (!matcher.find()) {
    // Do something
}

Alternatively:

if (!line.matches(".*\\b(eggs|bacon|cheese)\\b.*")) {
    // Do something
}

You have to double up the backslashes to escape them inside a String.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
0

For regular expressions pretty decent description is available from Java documentation

Also try this website to try out your pattern. It will allow you to quickly view how it behaves.

public static boolean matches(String regex, CharSequence input)

Compiles the given regular expression and attempts to match the given input against it.

An invocation of this convenience method of the form

 Pattern.matches(regex, input);

behaves in exactly the same way as the expression

 Pattern.compile(regex).matcher(input).matches()

If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.

Parameters:

  • regex - The expression to be compiled
  • input - The character sequence to be matched

Returns:

  • whether or not the regular expression matches on the input

Throws:

  • PatternSyntaxException - If the expression's syntax is invalid
hrust
  • 734
  • 1
  • 11
  • 34