0

I'm trying to get part of my Reg Exp (pcre flavor) working.

Here's the HTML structure. I want to select both ChildA and ChildB. (ie Match1: From Parent to ChildA; and Match2: From Parent to ChildB).

<Parent>
            <ChildA>..</ChildA>
            <ChildB>..</ChildB>

I tried

/<parent>(.|\n)*?<child(a|b)>/gmi

I tried with and without the question mark. It selects either up to ChildA or ChildB but not both.

Can anyone guide me please?

Here's a fiddle

Below are the matches I'm trying to get (clarifying as requested by Petr Srníček):

Match 1:

<Parent>
            <ChildA>

Match 2:

<Parent>
            <ChildA>..</ChildA>
            <ChildB>
Emily Rose
  • 95
  • 8
  • why you want to use regex? – Avinash Raj May 26 '16 at 09:52
  • In what language are you doing using regexes? In Perl you could use //g – Zafi May 26 '16 at 10:10
  • I don't understand what you're expecting... – SamWhan May 26 '16 at 10:12
  • The question seems unclear, can you edit it to show the exact matches you want to get? – Petr Srníček May 26 '16 at 10:14
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Tom Lord May 26 '16 at 10:14
  • 1
    Your regex will never match the input - the case is wrong and you haven't used the case insensitive flag – Bohemian May 26 '16 at 10:21
  • @Bohemian you can look at the OP's fiddle to see the flags used. – Chris Lear May 26 '16 at 11:02
  • Since your edit, I think what you're asking for is impossible. Your match2 does not exist in the original text, so can't be matched. – Chris Lear May 26 '16 at 11:04
  • Thanks everyone, I'll clarify my question with your questions. Avinash – I'm not aware of another way to select text by matching a pattern other than RegExp. Do let me know if there is another way. Alex – I'm using Sublime Text but happy even if it works Fiddle link in my post. It's for a manual cleanup, not built into anything specific. I've included /g but didn't return the result I want. Petr – I've added the matches I want as requested. Bohemian – the /i for is set in the tool, so I hadn't added in my post but it's in the Fiddle link. Now added to my my post too. – Emily Rose May 26 '16 at 11:09
  • @Chris Lear – This is awesome! Exactly what I was trying to do. Thank you Chris, you're a star! – Emily Rose May 26 '16 at 11:12

1 Answers1

1

This might do what you need:

/(<parent>.*?<child[ab]>).*?<child[ab]>/gmis

Note, I've added the s modifier and changed from (a|b) to [ab] to avoid unnecessary capturing groups.

See https://regex101.com/r/eL2sO6/2

Also note that match1 is as you requested. Your match2 will be the whole match. Add brackets round everything if you explicitly want it to be a submatch.

Chris Lear
  • 6,592
  • 1
  • 18
  • 26