5 Replies Latest reply: Oct 28, 2010 9:27 AM by 802340 RSS

    Help with Pattern Match!

    392786
      Could someone help me what would be the pattern to compile if I want to match any string in my xml that starts with "<?" and ends with "?>" ..and then replace it with blank space? How would I achieve it?
        • 1. Re: Help with Pattern Match!
          796440
          http://download.oracle.com/javase/tutorial/essential/regex/
          http://www.regular-expressions.info/tutorial.html
          • 2. Re: Help with Pattern Match!
            796440
            boiscout wrote:
            I want to match any string in my xml that starts with "<?" and ends with "?>"
            So, for instance, you want to match
            <?  ?> ?>
            and
            <? <? ?> ?>
            each in their entirety, each as a single match, right? That's what your statement says.
            • 3. Re: Help with Pattern Match!
              392786
              I got it. regex tester helped.
              Pattern.compile("<\\?.*\\?>")
              • 4. Re: Help with Pattern Match!
                796440
                Cool, glad you got it.

                So, to reiterate:


                So, for instance, you want to match
                <?  ?> ?>
                and
                <? <? ?> ?>
                each in its entirety, and each as a single match?

                And also
                <? abc ?> <other>  stuff </other> <? xyz ?>
                as a single match, the whole thing?

                Edited by: jverd on Oct 26, 2010 4:46 PM
                • 5. Re: Help with Pattern Match!
                  802340
                  boiscout wrote:
                  I got it. regex tester helped.
                  Pattern.compile("<\\?.*\\?>")
                  Let me summarize what that matches:

                  - it matches the first `<?` then the `.*` will match the entire line after that first `<?` (the DOT does not match line breaks!) and it will then back-track to the last `?>`

                  So, what jverd is trying to tell you, that greedy matching will cause:
                  <? B ?> C <? D ?>
                  to be replaced in a string like:
                  A <? B ?> C <? D ?> E
                  If that's want you want, well, fine by me. If not, you may want to make the DOT-STAR reluctant (non-greedy) by adding a question mark after it.