Everybody wants to tell you not to use a regex. And I agree. Regexes are a lot like nail guns. They can be extremely useful when applied properly, but I wouldn't use one to assemble a bird house. There's too much chance I'd nail my hand to my workbench.
Neverthess, I'm going to assume that you are actually required to use a regex. Could be a homework assignment. Could be a work assignment where the resident genius "knows" that a regex is the way it
must be done. Whatever.
There isn't really a regex code for "special character" as such. There are too many different kinds of "special". But accepting the idea that "special" means non-alphanumeric and going with the premise that you're starting from the beginning of a string, here's one possibllity:
Look for the largest continously alphanumeric sequence: "^([\p{alnum}]*)" - remember that in an actual
Java string expression, you have to double-up on the backslashes. This regex only looks for USA ASCII simple letters and numbers - to it, the extended letters and other characters are "special"
Another option looks for word-like charactersL "^(\w*)". However, that one includes the underscore character, which means that it might not be "special" enough.
You can also do brute-force: "^([a-zA-Z0-9]*)".
Various other regex options exist. Note that there's also no "universal" regex language definition set, so what's a vaiid regex in Java might not be valid using the Unix grep utility or
vice versa.
Then again, as others have said, regex isn't always the most elegant solution to string parsing. Various other parsing and scanning services exist in Java and might be simpler to use, depending on your exact needs and constraints.