Saturday, March 10, 2012

Groovy: Regular Expressions and Multiple Lines

Regular expressions are sometimes used on text that spans multiple lines, which Groovy has support for. I had to search for how to enable multi-line searching, so I thought it would be good to post here.

To allow the expression to span multiple lines we can add (?ms) to the beginning of the expression.

Take the following example, where want to grab the entire book element from a single-line XML document:

    def xml = "<library><book><title>Effective Java</title><author>Bloch</author></book></library>"
def matcher = xml =~ /<book>.*<\/book>/
matcher.size() > 0 ? matcher[0] : "NOTHING"

This gives us an entire book:

    <book><title>Effective Java<title><author>Bloch<author><book>

If the XML was to span multiple lines however, we end up with NOTHING:

    def xml = """<library>
<book>
<title>Effective Java</title>
<author>Bloch</author>
</book>
</library>"""
def matcher = xml =~ /<book>.*<\/book>/
matcher.size() > 0 ? matcher[0] : "NOTHING"

Now if we add the (?ms), we will still get an entire book entry:

    def xml = """<library>
<book>
<title>Effective Java</title>
<author>Bloch</author>
</book>
</library>"""
def matcher = xml =~ /(?ms)<book>.*<\/book>/
matcher.size() > 0 ? matcher[0] : "NOTHING"

The above results in:

  <book>
<title>Effective Java<title>
<author>Bloch<author>
<book>

If you have any other tips or another way to accomplish the same thing, please feel free to leave a comment.

30 comments:

  1. THANK YOU SO MUCH
    EXACTLY WHAT I WAS LOOKING FOR

    ReplyDelete
  2. Thanks for providing the best information it's very useful sap basis learners.

    SAP BASIS Course in Hyderabad
    Online SAP BASIS Training in USA

    ReplyDelete
  3. Yes ,

    This is interesting blog and valuable blog.thanks for sharing such a important blog.
    oracle fusion procurement online training

    ReplyDelete
  4. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor
    lead live training in Oracle Fusion Financials online Training , World Class Virtual Instructor led training on Oracle fusion Financials Training. We have industry expert trainer. We provide Training Material and Software Support. calfre has successfully conducted 100000+ trainings in India, USA, UK, Australlia,
    Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.

    ReplyDelete
  5. You're so interesting and Fantastic; so nice to find someone with some original thoughts on this subject seriously........................Erptree Oracle Training Institute.

    ReplyDelete

  6. I am overwhelmed by your post with such a nice topic. Usually, I visit your blogs and get updated with the information you include but today’s blog would be the most appreciable...

    Thanks
    Cpa offers

    ReplyDelete
  7. Hiiii...Thanks for sharing great info...Nice post...Keep move on...
    Blockchain Training in Hyderabad

    ReplyDelete
  8. thanks for sharing nice information -------
    more : http://opensourcetechnologies.in/blockchain-training-in-hyderabad

    ReplyDelete
  9. I am happy for sharing on this blog its awesome blog I really impressed. thanks for sharing.

    Became an Expert In Google Cloud Platform Training in Bangalore! Learn from experienced Trainers and get the knowledge to crack a coding interview, @Softgen Infotech Located in BTM Layout.

    ReplyDelete
  10. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.Real Time Experts Training in Bangalore

    ReplyDelete
  11. Nice Blog.
    Thanks for sharing.
    We help IT professionals by providing them Best Online Job Support in 250+ technologies. Our services are very reliable and most affordable. Call Today for free demo.

    ReplyDelete
  12. Great Share!!!Java concepts are really Amazing...Explanation's are make me to clear all my doubts...Big Thanks for your effective works...
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  13. Wonderful article, very useful and well explanation. Your post is extremely incredible. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    Java Training in Chennai

    Java Training in Velachery

    Java Training inTambaram

    Java Training in Porur

    Java Training in Omr

    Java Training in Annanagar

    ReplyDelete
  14. This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
    DevOps Training in Chennai

    DevOps Course in Chennai

    ReplyDelete
  15. thanks for assisting human beings income the information they mannerism. brilliant stuff as pleasing. preserve up the massive take feature!!!! https://cyberspc.com/tally-erp-9-crack/

    ReplyDelete
  16. Excellent blog and I really glad to visit your post. Keep continuing...
    Embedded Systems Course in Hyderabad

    ReplyDelete
  17. This comment has been removed by a blog administrator. our sclinbio.com

    ReplyDelete
  18. This is the good website and also i love this website https://sclinbio.com

    ReplyDelete