I've started using the .match(Regex)
method in my java program, but for now I'm just using a string (String regexString = new String("[^a-zA-Z0-9][^a-zA-Z0-9]*");
which is what I have so far, as an example). I know however I can use an actual regex (Regex pattern = new Regex() & the Pattern class
then compile it (?) some how).
Is there an advantage to using Regex as a class and not just a string, in java? I'm quite used to bash scripting and there regexes are just 'strings' in the loosest sense, and there is no ability/need for a separate class, so I'm struggling to see where there is one here.
So, yes, regular expressions really only apply to strings. If you want a more complicated FSM, then it's possible to write one, but not using your local regex engine.
$ means "Match the end of the string" (the position after the last character in the string).
I would do what you think is simplest and clearest.
A Pattern
is often used when the performance of a regex is critical. If you haven't profiled your application and it has been shown to be an issue, using a plain String
is likely to be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With