Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex help on an incoming XML stream  RSS feed

 
Anthony Campbell
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have a quick question regarding some regex. I understand the concept and how it works however, I always have trouble when fine tuning my expressions. I definitely need more exposure! What I need to do is very simple. I need to search an XML stream for numbers of a certain length (or other similar criteria). For example:

\d(10,100)

Would match a number between 10 and 100 in length. The bit I can't get my unit tests to pass is where I need the number to fall into a certain element. Let's say "test". So my regex would be something like:

<test>\d(10,100)</test>

I'm not sure how to group the element tags and the digits together. I thought it would be as simple as:

(<test>\d(10,100)</test>)

Could anyone expand, or just drop me a few tips. Thanks!
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony,

A big problem I see right off the bat is that your regex is wrong. I think you meant to do something like this:


In regexes, parentheses are for grouping, not for counts. For those, you need curly braces, i.e., '{' and '}'

I would do your second regex like this:


Note that in Java, regexes are strings, so you'll need to escape your backslashes for them to be recognized. BTW, O'Reilly has a very good book called Mastering Regular Expressions by Jeffrey E. F. Friedl that will tell you everything you need to know about regular expressions (and more). It's got a very good rundown on Java's regex implementation, as well as those in several other languages.

John.
 
Anthony Campbell
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thanks. I think I may also chase up the book you recommend. Thanks again,
Anthony
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!