T
T
The_XXI2021-04-13 16:32:33
Java
The_XXI, 2021-04-13 16:32:33

How to check if a string only consists of certain characters?

I need to do 3 checks:

  1. the string consists only of letters and the following characters: space and hyphen;
  2. the string consists only of a digit and the following characters: plus, minus, opening and
    closing parentheses, space character;
  3. the string must be validated against a valid email.

I've read about regular expressions, but haven't been able to figure them out yet.
How can all this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-04-13
@The_XXI

It can be implemented with regular expressions.
Here you can test them: https://regex101.com/
For example: "1. the string consists only of letters and the following characters: space and hyphen"
Regular: [a-zA-Z\-\s]
Test string: sdlfkj -sdfgklj ifds
By trial and error, you will recover in 6-8 hours.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question