java - Help with regex -
i'm constructing regex accept @ least 1 alpha numerical character , number of spaces.
right i've got...[a-za-z0-9]+[ \t\r\n]* understand @ least 1 alphanumeric or @ least 1 space. how fix this?
edit: answer comments below want accept strings contain atleast 1 alphanumeric , number of (including no) spaces. right accept whitespace.
edit2: clarify, don't want number of whitespace (including 0) accepted unless there @ least 1 alphanumeric character
\s*\p{alnum}[\p{alnum}\s]* your regex, [a-za-z0-9]+[ \t\r\n]*, requires string start letter or digit (or, more accurately, doesn't start matching until sees one). adding \s* allows match start whitespace, still won't match alphanumerics after first whitespace character follows alphanumeric (for example, won't match xyz in abc xyz. changing trailing \s* [\p{alnum}\s]* fixes problem.
on side note, \p{alnum} equivalent [a-za-z0-9] in java, not case in regex flavors. used \p{alnum}, not because it's shorter, because gives more protection typos [a-z] (which syntactically valid, not author meant).
edit: performance should considered, too. included + after first \p{alnum}, realized wasn't idea. if part of longer regex, , regex didn't match right away, end wasting lot of time trying match same groups of characters \p{alnum}+ or [\p{alnum}\s]*. leading \s* okay, though, because \s doesn't match of characters \p{alnum} matches.
Comments
Post a Comment