0

Hi there I'm new to Java and was going through some information on regex and I couldn't comprehend this the following expression:

"^[a-zA-Z\-]+$"

Could someone be kind enough to explain each and every character in this expression?

Thank you.

FPOC
  • 25
  • 5
  • 4
    Does this answer your question? [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – OH GOD SPIDERS Apr 15 '20 at 10:16

2 Answers2

3
^           $  # Check if the entire string matches,
 [        ]+   # with one or more of the following characters:
  a-z          #  Any lowercase (ASCII) letter
     A-Z       #  Any uppercase (ASCII) letter
        \-     #  Or an "-" (the `\` is used to escape it)

Or in short: this regex checks if a given string consists solely of (ASCII) letters and/or -, and is non-empty.

Try it online.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
1

[a-zA-Z] means all characters a through or A through Z, inclusive.
The "\" inside the square bracket is used as an escape character.
Symbol "+" in the end signified that your regex can occur once or more times.

Ugnes
  • 709
  • 1
  • 9
  • 23