Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-insensitive string matching in Java without anchors

NOTE: This is NOT a question about case-insensitive matching. It is a question about regex anchors.

I'm having a lot of trouble doing basic case insensitive matching in Java:

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

public class match {
    public static void main(String[] args) {
        String prompt="das101.lo1>";
        String str="[email protected]>";

        Pattern ignore = Pattern.compile(prompt.toUpperCase(), Pattern.CASE_INSENSITIVE);
        Matcher mIgn  = ignore.matcher(str);
        if(mIgn.matches())
            System.out.println(str+" Matches " + prompt.toUpperCase());
        else
            System.out.println(str+" Doesn't Match " + prompt.toUpperCase());

        char[] cStr = str.toCharArray();
        char[] cPrompt = prompt.toUpperCase().toCharArray();

        /* Verify that strings match */
        for(int i=cPrompt.length-1, j=cStr.length-1; i>=0 && j>=0 ; --i,--j) {
            if (cPrompt[i]==cStr[j])
                System.out.println("Same: "+ cPrompt[i]+":" + cStr[j]);
            else
                System.out.println("Different: "+ cPrompt[i]+":" + cStr[j]);
        }
    }
}

The output:

samveen@javadev-tahr:/tmp$ javac match.java
samveen@javadev-tahr:/tmp$ java match
[email protected]> Doesn't Match DAS101.LO1>
Same: >:>
Same: 1:1
Same: O:O
Same: L:L
Same: .:.
Same: 1:1
Same: 0:0
Same: 1:1
Same: S:S
Same: A:A
Same: D:D

If I change if(mIgn.matches()) to if(mIgn.find()), I get this simple string pattern match working:

samveen@javadev-tahr:/tmp$ javac match.java
samveen@javadev-tahr:/tmp$ java match
[email protected]> Matches DAS101.LO1>
Same: >:>
Same: 1:1
Same: O:O
Same: L:L
Same: .:.
Same: 1:1
Same: 0:0
Same: 1:1
Same: S:S
Same: A:A
Same: D:D

Where am I going wrong?

I referred to Case-Insensitive Matching in Java RegEx and Methods of the Pattern Class

like image 993
Samveen Avatar asked Oct 16 '14 14:10

Samveen


People also ask

How do you make a string case insensitive in Java?

Java String equalsIgnoreCase() Method The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase() method to compare two strings lexicographically, ignoring case differences.

Is Java regex case sensitive?

Java Regular Expressions are case-sensitive by default. But with the help of Regular Expression, we can make the Java Regular Expression case-insensitive. There are two ways to make Regular Expression case-insensitive: Using CASE_INSENSITIVE flag.

Is there containsIgnoreCase?

containsIgnoreCase is a static method of the StringUtils class that checks whether a given string contains the complete search string as a whole, while ignoring the case considerations. The comparison is case-insensitive in nature. Refer to What is StringUtils.

What is case sensitive and case insensitive in Java?

Java is a case-sensitive language, which means in code showData and showdata are two different variables. Java is case-sensitive because it uses a C-style syntax. In most programming languages, case sensitivity is the norm. Case-sensitive is useful because it lets you infer what a name means based on its case.


1 Answers

String.matches requires the entire string to match the pattern. As if the pattern has an implied "^...$".

Pattern ignore = Pattern.compile(".*" + Pattern.quote(prompt) + ".*",
    Pattern.CASE_INSENSITIVE);

is for a find like match.

This could have been done with the original pattern as:

if (mIgn.find()) {
    System.out.println("Found at position " + mIgn.start());
}
like image 67
Joop Eggen Avatar answered Nov 09 '22 23:11

Joop Eggen