Skip to main content

Java Regular Expression

In Java programming, how do we check whether the following text contains a piece of information encoded in an <ns1:Elmt> XML element?

This is a preamble #@&%)#*#)$

<ns1:Elmt ns1="uri:ns1" ns2="uri:ns2">
  <ns1:SubElmt attr1="value_1">
    <ns2:elmt>value_2</ns2:elmt>
  </ns1:SubElmt>
</ns1:Elmt>

Here is a little bit more text

3254832 3249028 ~#)*@()(84)_$-

An answer: Use the string.matches() method and the following regular expression
[\s\S]*<ns1:Elmt[\s\S]*</ns1:Elmt>[\s\S]*

Check the following websites and you'll learn how regular expression can ease our programming problems!

Wikipedia : Regular Expression
java.util.regex.Pattern (Java 2 Platform SE 5.0)
Regular-Expressions.info Tutorial

Keywords: ,

Comments

Popular posts from this blog

Lenovo IdeaPad S10: Annoying Fan Noise Fix and Heat Conduction Upgrade

Here's a repost of my message on the Lenovo.com board: I would like to thank Slash (on Lenovo.com board) so much for his messages on this thread: http://forums.lenovo.com/lnv/board/message?board.id=IdeaPad_Netbook&message.id=1183#M1183 I decide today (February 28, 2009) to walk into a Lenovo Service Center inside a computer mall in downtown Bangkok because I cannot tolerate the constant grinding noise from my S10 anymore. I bought my S10 in late November 2008 and I started to hear some grinding noise in late December 2008. I have been using the S10 as my primary computer both for work and for home. At work, this noise has embarrassed me many, many times. At home, I left the S10 with Vista in High Performance mode on overnight so that it can complete the maintenance tasks, e.g., HDD Defrag and Indexing, but the fan noise from high CPU temperature woke me up at 4:45 in the morning! I learned from Slash's picture http://i41.tinypic.com/25alq3d.jpg that Lenovo chose to use chea

iTunes: Error 261 while Burning Data CDs/DVDs

Apple Discussions: 261 error while burning MP3 CD. I usually put an aphostrophe (') in the name of playlists and was having error 261 burning CD/DVD data discs since iTunes 5.1. Upgrading to iTunes 6.0 for Windows doesn't help. I was burning a music DVD this morning and had Error 261 again. So I went to Apple iTunes Discussions site and look for a thread on this. Viola! There are many people having the same problem as mine. The thread ends at a point where someone removing ampersands, the '&' symbols, from their playlists and could avoid this error. I tried removing aphostrophes from the name of my playlist because aphostrophe falls into the kind of non-alphabet characters that needs escaping in some programming languages. It works!! Keywords: itunes , windows

Microsoft Office Outlook Connector 12.1 Beta 2 Crashes Thai Language Input in Outlook 2007

It's been more than a year since I last blogged. I've been busy with work and also don't know what to blog. Here comes my new findings: Microsoft Office Outlook Connector 12.1 Beta 2 + Outlook 2007 + Thai Language Input = CRASH This has also been proven here . I downloaded the Outlook Connector the other night for improved supports to Windows Live Hotmail -- only to find that whenever I start typing e-mails in Thai, i.e., hit the "language switch" key Outlook simply crashes! I googled for " outlook 2007 crash language switch " this morning and find someone having the same Outlook Connector insta.led and the same problem as mine. So, Outlook Connector is most likely the cause of trouble. I uninstalled Outlook Connector this morning and, yes, typing in Thai doesn't crash Outlook 2007 anymore! Keywords: Outlook , Thailand , Troubleshooting