im currently doing a little project for school. I have to check if a string is of the following type:
"One or more whitespaces" - "On ore more whitespaces"
For example: String s = " - "
I could trim the string and check only for "-"
, but that doesnt work because "-"
should be handled differently than " - "
I currently tried something like this:
if (f.equals(" {1,} - {1,}"))..
but i have no idead what that is exactly doing and it doenst work either.
Can someone help me out?
Shari
答案 0 :(得分:0)
Try this regex: "^\\s+-\\s$+"
You can use String.matches()
, because .equals
doesn't do regexes.
"Untainted" version:
^\s+-\s+$
^
and $
anchor to the ends of the string.
\s+
matches one or more whitespace(s).
答案 1 :(得分:0)
You need to use groups. Use (
and )
to establish groups.
To answer your question I think this would work:
^(\s+) - (\s+)$
(\s+|\s*) - (\s+|\s*)
the *
means 0 or more
the +
means 1 or more, so if you're matching 1 or more, you should have no white spaces around your pattern ie: -
A very good 'toy' is this. Just hovering over the characters shows you details, you have a cheat-sheet and many more!
答案 2 :(得分:0)
reg = "\s+-\s+"
这可能会这样做。