Java Regex to Match words + spaces -
i trying construct simple regex match words + whitespace in java, got confused trying work out. there lot of similar examples on site, answers give out regex without explaining how constructed.
what i'm looking line of thought behind forming regular expression.
sample input string:
string tweet = "\"whole lotta love\" - led zeppelin";
which when printed is: "whole lotta love" - led zeppelin
problem statement:
i want find out if string has quotation in it. in above sample string, whole lotta love
quotation.
what i've tried:
my first approach match between 2 double quotes, came following regex:
"\"(\\w+\")"
, "\"(^\")"
but approach works if there no spaces between 2 double quotes, like:
"whole" lotta love
so tried modify regex match spaces, , got lost.
i tried following, don't match
"\"(\\w+?\\s+\")"
, "\"(\\w+)(\\s+)\""
, "\"(\\w+)?(\\s+)\""
i appreciate if me figure out how constuct this.
you had it. regexes match alphanumeric characters followed spaces, this:
"whole "
but not alphanumeric chars after that. 0 right, want use capture this:
"\"([\\w\\s]+)\""
this matches 1 or more [whitespace/alphanumeric] chars. note alphanumeric includes _
.
if want more general, use
"\"([^\"]+)\""
which match everything besides double quotes. instance, "who's on first?" (including quotes) matched second regex not first, since includes punctuation.
Comments
Post a Comment