asdasd asdasd - 1 year ago 86
Java Question

How to extract a substring using regex

I have a string that has two single quotes in it, the

character. In between the single quotes is the data I want.

How can I write a regex to extract "the data i want" from the following text?

mydata = "some string with 'the data i want' inside";

Answer Source

Assuming you want the part between single quotes, use this regular expression with a Matcher:



String mydata = "some string with 'the data i want' inside";
Pattern pattern = Pattern.compile("'(.*?)'");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())


the data i want
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download