Andrejs
Andrejs

Reputation: 11901

How to pass a RegEx expression as a parameter in Java?

What is the easiest way to construct a java regular expression and pass it to a method that expects a String? Ideally something similar to SO JavaScript solution here.

System.out.println(System.getProperty(/* Regex here*/));

Upvotes: 1

Views: 2834

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074088

What is the easiest way to construct a java regular expression and pass it to a method that expects a String?

You can't. You can only use regular expressions with methods that are written to accept them, you can't use them with any method that just accepts a string (such as System.getProperty).

Methods accepting regular expressions, either explicitly as Pattern instances or implicitly as strings (like String#replaceAll), will say clearly that they do in their JavaDoc.

Upvotes: 1

Related Questions