0

I have a text and I want to extract Parentheses of text with regex java.
for example of text:

<p>Now a days, regenerative medicine(1) in stem cell(3) filed
   widely <label>attractive(10) by</label> attractive by scientists(4).</p>

I want to extract Parentheses of text if Parentheses no exist between label tags.
for example of extract above text:

(1)
(3)
(4)

it should not extract parentheses between label tags.
i use of regex following :

 (<label>){0,1}[(\\w\\W)&&[^[</label>|\\(|\\)]]]*(\\(\\s*[(\\w\\W)&&[^\\(\\)]]+\\)) 
 [(\\w\\W)&&[^[</label>|\\(|\\)]]]*(</label>){0,1}

1 Answer 1

1
public static void main(String[] args) {
        String in = "<p>Now a days, regenerative medicine(1) in stem cell(3) filed widely <label>attractive(10) by</label> attractive by scientists(4).</p>".replaceAll("<label>.*</label>", "");;
        //String inin = in.replaceAll("<label>.*</label>", "");
        //System.out.println(inin);
        Pattern p = Pattern.compile("\\((.*?)\\)");
        Matcher m = p.matcher(in);

        while(m.find()) {
            System.out.println("(" + m.group(1) + ")");
        }
    }

Output:

(1)
(3)
(4)

I am just ignoring the text inside the label tag and then taking the text which is inside bracket.

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

4 Comments

it should not extract parentheses between label tags
(10) should not be there as it is between <label> tag
OK. I guess a clean approach would be to separate this into two steps: (A) remove the label elements with all its children and (B) extract the numbers. (B) can be done as described above. (A) can be done using SAX, XSLT, XQuery or some other XML processing mechanism.
@user986566 I edited my answer . I think this will solves your problem.

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.