Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find exact match from Array

Tags:

java

In java I want to iterate an array to find any matching words from my input string

if the input string is appended to numbers it should return true.

Array arr = {"card","creditcard","debitcard"}
String inputStr = "need to discard pin" --> Return False
String inputStr = "need to 444card pin" --> Return True if its followed by number

I tried the below code, but it returns true as it takes "card" from the "discard" string and compares, but I need to do an exact match

Arrays.stream(arr).anymatch(inputString::contains)

like image 498
Sadu Avatar asked Jun 24 '21 12:06

Sadu


3 Answers

Try this:

String[] arr = {"card","creditcard","debitcard"}; // array that keeps the words
String inputStr = "need to discard pin"; // String that keeps the 'sentence'

String[] wordsToBeChecked = inputStr.split(" "); // We take the string and split it at each " " (space) 

HashSet<String> matchingWords = new HashSet<>(); // This will keep the matching words

for (String s : arr) 
{
    for (String s1 : wordsToBeChecked) 
    {
        if(s.equalsIgnoreCase(s1)) // If first word matches with the second
        {
            matchingWords.add(s1); // add it to our container
        }
    }
}

Or using Java 8 Streams:

List<String> wordList = Arrays.asList(arr);
List<String> sentenceWordList = Arrays.asList(inputStr.split(" "));

List<String> matchedWords = wordList.stream().filter(sentenceWordList::contains)
                                               .collect(Collectors.toList());
like image 70
Renis1235 Avatar answered Oct 09 '22 07:10

Renis1235


The problem with most answers here is that they do not take punctuation into consideration. To solve this, you could use a regular expression like below.

String[] arr = { "card", "creditcard", "debitcard" };
String inputStr = "You need to discard Pin Card.";

Arrays.stream(arr)
    .anyMatch(word -> Pattern
        .compile("(?<![a-z-])" + Pattern.quote(word) + "(?![a-z-])", Pattern.CASE_INSENSITIVE)
        .matcher(inputStr)
        .find());
  • With Pattern.quote(word), we escape any character within each word with is a special character in the context of a regular expression. For instance, the literal string a^b would never match, because ^ means the start of a string if used in a regular expression.

  • (?<![a-z-]) and (?![a-z-]) mean that there is not a word character immediately preceding or succeeding the word. For instance, discard will not match, even if it contains the word card. I have used only lowercase in these character classes because of the next bullet:

  • The flag CASE_INSENSITIVE passed to the compile method causes the pattern to be matched in a case-insensitive manner.

Online demo

like image 1
MC Emperor Avatar answered Oct 09 '22 06:10

MC Emperor


You could split the string using a regular expression

String[] arr = {"card","creditcard","debitcard"};
String inputStr = "need to discard pin";

List<String> wordsToBeChecked = Arrays.asList(inputStr.split("[ 0-9]"));

Arrays.stream(arr).anyMatch(wordsToBeChecked::contains);

If your word list and input string is longer, consider splitting your input string into a hashset. Looksups will be faster, then:

Set<String> wordsToBeChecked = new HashSet<>(Arrays.asList(inputStr.split(" ")));
like image 1
Mario Varchmin Avatar answered Oct 09 '22 06:10

Mario Varchmin