0

I am new to regex parsing in java. I want to parse the string which contain the records. But I want to select the selected part of that record only.

\"6\":\"Services Ops\",\"practice_name\":\"Services Ops\",\"7\":\"Management\",

For this, I have written regex expression as

(^\\\"6\\\":\\\"[A-Za-z \s]*)

and above expression gives me result as : \"6\":\"Services Ops\

I want only Service Ops

And also there are multiple records like \"5"\:\"xxx"\ and so on thus if I write the expression for only Service Ops then entries from other fields are also included in the result of the expression.

Is there any way that we can select the string which start with some pattern but we can exclude that pattern.

Like in above example, string starting with \"6\":\" but we can exclude this part and get only Service Ops as result.

Thank you.

1
  • That input looks suspiciously like json. Commented Jul 30, 2013 at 5:11

1 Answer 1

3

You can use lookarounds which perform only a check but don't match:

lookahead (?=...)
lookbehind(?<=...)

example:

(?<=\\\"6\\\":\\\")[^\"]++(?=\")

An another way is to use a capturing group (...):

\\\"6\\\":\\\"([^\"]++)\"

Then you can extract only the content of the group. Example:

Pattern p = Pattern.compile("\\\"6\\\":\\\"([^\"]++)\"");
Matcher m = p.matcher(yourString);
if (m.matches()) {
    System.out.println(m.group(1));
}
Sign up to request clarification or add additional context in comments.

Comments

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.