6

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.

3
  • 2
    Never write new String("...");. Just use "..." instead: String regexString = ".*";. It's unnecessary and inefficient to explicitly create a new String object. Commented Oct 3, 2012 at 8:18
  • Is an object not created whenever I do that anyway? What if I have several separate strings? Commented Oct 3, 2012 at 8:21
  • 3
    Yes, if you just use a literal, for example ".*", a String object is created. But if you do new String(".*"), a second String object is created which copies the content of the literal string, which is unnecessary. Strings are immutable in Java. Java has a string pooling mechanism, so that if you use the same string literal multiple times, there will be only one String object which is shared. Commented Oct 3, 2012 at 8:24

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

One thing that I've relised is that you can only match with a string, you can't find which is something I'd like to be able to do.
You can match(".*"+regex+".*"); instead of find.
0

The regular expression you are trying to use for matching purposes must be transformed in a finit state machine that accepts any text that matches the pattern you provided.

Thus, every time you use a regular expression, for example in the (new String("").split("") expression, a pattern is constructed in the background, namely a finite state machine, which receives the sequence of characters in your string and tries to match the input.

If you have a regular expression that you use often and speed is a real concern, then keeping the finite state machine and not constructing it every time is an important speedup. You can do so by storing the Pattern object between successive calls of match onn different output strings.

The following should provide some more insight: http://en.wikipedia.org/wiki/Finite-state_machine

1 Comment

I don't see how initializing a string that is transformed into a pattern that is stored is so much different to a pattern that initialized from a string and stored...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.