1

So I want to match a string to a simple mask but I would like to avoid using regex because my client have access to this mask and can change it.

Is it possible to have a string matches something like #####-000?

Or I have no choice but to convert my simple mask to regex ?

Thank you.

7
  • Can you give us an example where you want to match it? Commented Mar 14, 2016 at 14:08
  • This looks like a number format but in case it isn't you'd either have to convert it to a regex (shouldn't be too hard) or find/create a matching utility. Commented Mar 14, 2016 at 14:11
  • @BenjaminLücking I want to match it like: 12345-000 is true, 12345-001 is false, 1234-000 is false. ( is that what you meant? ) Commented Mar 14, 2016 at 14:13
  • 1
    The regex wouldn't be that hard: if # means a number then just use it like input.matchs("#####-000".replaceAll("#","[0-9]")) which should result int the expression [0-9][0-9][0-9][0-9][0-9]-000. Ofc you could also count the # and replace the while sequence with like [0-9]{5} instead. Commented Mar 14, 2016 at 14:16
  • 1
    If you want to check if a String matches a specific mask, you could iterate through every character in that String end check if the first characters are numbers and the remaining characters are -000 Commented Mar 14, 2016 at 14:19

1 Answer 1

1

If I get your question (and the comments) the right way, you could do the following:

  • Split your string at '-'
  • Check if string before '-' has length 5 and is numeric (StringUtils.isNumeric(...))
  • Check if the string after '-' equals '000'
Sign up to request clarification or add additional context in comments.

Comments

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.