简体   繁体   中英

To match a bar code: Add regex to a given string, whithout changing the string itself

I get a string in this format: 00123456

The string has always 8 digits, starts always with 2 zeros, and the other 6 digits can be anything from 1-9.

I have to compare this string to another string, which has the same structure, but without the 2 zeros. So, 00123456 should be equal to 123456. The problem is, that I don't have the possibility to change the strings (eg delete the zeros). The only thing I could do, is to add regex before or after the first string.

So in Java code, I need something like this, whithout changing the first string itself. I can only add something before and/or after the string:

    String givenStringWithRegex = "(ignore zeros)00123456";
    String userInput = "123456";
    if (userInput.matches(givenStringWithRegex)) {
        // Everything is fine :)
    }

Thank you in advance :)

Edit: Thank you for your answers so far, but the problem is, that I cannot change the code of the client application. The client app does userInputString.matches(givenStringWithRegex) and I cannot change this. Thats why I need another solution, for example adding regex to the given string without changing the string itself.

Edit2: This is the code in the client application and I can nothing change here. The "regex" variable is the given string and "scannedValue" is the user input (123456). The given string is in my concrete example 00123456. Like I wrote before, I cannot manipulate 00123456. I can only add regex before or after it, for example ???00123456???. And 00???123456 is not possible.

I can add this regex in a configuration file of the superior system, so no code change would be needed. Thats why I asked for a regex solution, because I don't want to change code in superior system and client application.

public final class RegexValidator implements StringScanValidator {

    private final String regex;

    public RegexValidator(final String regex) {
        this.regex = regex;
    }

    @Override
    public StringScanValidatorResult validate(final String scannedValue) {
        return scannedValue.matches(regex) ? new StringScanValidatorResult(true, scannedValue)
            : new StringScanValidatorResult(false, scannedValue);
    }
}

Just use substring() like this :

public static void main(String[] args) {
    String givenStringWithRegex = "00123456"; // always 8 digits
    String userInput = "123456"; //so, always 6 digits
    System.out.println(givenStringWithRegex.substring(2, 8).equals( // do substring for digits 2 to 8
            userInput));
}

O/P :

true
String givenString = "00123456".substring(2); 
String userInput = "123456";

 if(userInput.equals(givenString) { // Everything is fine :) }

Substringing will fit your need. It will cut a portion of the string that is relevant to you.

Can you do something like this:

String givenString = "00123456";
String userInput = "123456";
if (("00"+userInput).equals(givenString)) {
       // Everything is fine :)
}

Apart from your problem you never can change String s in java: they are immutable. But you can make any number of copies of an original String. So you can use substring to cut of the 0 s without changing the original String:

String original = "00123456";
String compare  = "123456";
String copy = original.substring(2);
if(copy.equals(compare)) {...}

An other way would be to prepend user-input with zeros:

String original = "00123456";
String compare  = "123456";
if(original.equals("00"+compare)) {...}

So using regular expresion seems like terrible overkill!

Strings are immutable. You could do something like:

String givenStringWithRegex = "(ignore zeros)00123456";
String userInput = "123456";
if (("00"+userInput).equals(givenStringWithRegex)) {
    //its equal and none of your strings are modified as string are immutable
}
public static String regexStr(String str) {
    Pattern p =
        Pattern.compile("00(\\d{6})", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); //start with 00 then followed by 6 digits
    Matcher m = p.matcher(str);
    if (m.find()) {
        String found= m.group(1);
        return found;
    }
    return null;
}

public static void main(String[] args) throws ParseException {
    String givenStringWithRegex = "00123456";
    String userInput = "123456";
    if (userInput.equalsIgnoreCase(regexStr(givenStringWithRegex))) {
        // Everything is fine :)
        System.out.println("Everything is fine");
    }
}

This is the code in the client application and I can nothing change here. The "regex" variable is the given string and "scannedValue" is the user input (123456). The given string is in my concrete example 00123456. Like I wrote before, I cannot manipulate 00123456. I can only add regex before or after it, for example ???00123456???. And 00???123456 is not possible.

 scannedValue.matches(regex) 

The regex rules make the fulfillment of your wish impossible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM