line.matches("[A-Z] ([a-zA-z][a-zA-Z]*)|\\#")
I need to read elements like
"A ddg", "B gH" or "D #"
But it doesn't work and I need to know if the regular expression is ok.
line.matches("[A-Z] ([a-zA-z][a-zA-Z]*)|\\#")
I need to read elements like
"A ddg", "B gH" or "D #"
But it doesn't work and I need to know if the regular expression is ok.
Try this:
line.matches("[A-Z] ([a-zA-Z]+|#)")
Much of your regex is redundant:
\\# is the same as # - there's nothing special about #[a-zA-Z][a-zA-Z]* is identical to [a-zA-Z]+The thing you were missing was the correct alternation - you didn't have the closing bracket in the right place.
You have this problems:
# outside of it,A-z range instead of A-Z. Also you can improve your regex a little:
#xx* you can use x+ since:
+ represents once or more * represents zero or moreSo try with
line.matches("[A-Z] ([a-zA-Z]+|#)")
This will work:
line.matches("[A-Z]( [a-zA-z]+)?( #)?")
What will match (examples):
A
A #
A a
A aAaA
A a #
A aAaAa #
If you don't want "A #":
line.matches("[A-Z]( [a-zA-z]+| #)")