Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with Pattern matching

 
kranthi chamarthi
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am reading an xml then converting it into a String. Now I have an Element in this xml which is converted into String, shown as below

I am trying to use pattern matching to get all the attributes and their corresponding values in this Element which are having $
I am trying to use the pattern

But when i use matcher.group() method it gives output as

where as the expected output is

I tried to add some Reluctant quantifiers but, was not able to resolve this. Any inputs will be helpful.

Thanks
Kranthi
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use a different technique than reluctant qualifiers in this case. I use negation.

This code does what you want.


Let's look at why:
[0-9a-zA-Z] - match the first character in the attribute. Note the quote is not needed in here.
[^=]+ - anything until you hit the equals sign
= - equal sign
\" - opening quote
[^\"]+ - anything until you hit the closing quote
\" - closing quote

Note that while this regular expression works for your example, it doesn't work for all valid XML. For example, what if I have a space between the = and the "? This is why it is better to use an XML parser. You can pass in a String to be parsed as DocumentBuilder and the like take an InputSource or InputStream as a parameter. And there is a ByteArrayInputStream that you can create with just a String.

The XML Parser is very robust given that it is built into Java. ANd it has APIs to get attribute names/values.
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kranthi chamarthi wrote:I am reading an xml then converting it into a String...[and]...I am trying to use pattern matching to get all the attributes and their corresponding values in this Element

Well, that right there is a major red flag.

Regexes, although they might seem great (and are great for many things), were never designed for contextual or recursive parsing, which is what you need when you're dealing with XML. As Jeanne said, you're much better off using a proper parser like DOM or SAX.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I worked in a scene shop, we did a lot of carpentry/construction of set pieces. Everything from walls to doors to furniture to hand props...The shop lead would always tell us "Use the right tool for the right job".

Using a regex to parse XML is like using a jig-saw to drive in a screw. It's really not your best bet.
 
kranthi chamarthi
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for late reply... But my requirement is to find out all attributes in input xml which has attribute value in a pattern like ${VariableName}. Then i will get the corresponding value for this VariableName from a collection and substitute this as the value for the attribute. Its like i make an api call, get output, i have to use some values in this output and prepare the input dynalically to the next api call. I thought rather than using DOM to traverse through xml and check for each attribute in each element for value in format ${VariableName}, it will be better performance wise to use Pattern matching, to get the attributes for which values needs to be populated dynamically.

I agree that writing correct regex pattern for this task is a bit error prone.
 
Jhon Gonzales
Greenhorn
Posts: 22
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this:



 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kranthi chamarthi wrote:Its like i make an api call, get output...

Oh no it isn't. And don't let any XML-phile tell you that it is.

For one thing: inside Java (and sometimes even outside) there is NO conversion involved. You simply transmit the data.

Strings are NOT "objects" (except in the broadest sense), and XML adds a whole new layer to the business of conversion - not the least being that it ALL has to be "well-formed".

What XML does allow you to do (given all the above constraints, AND with the right encoding and parser) is to transmit anything in context (IF that context is understood by the receiver). But the overheads are huge.

However, getting back to your actual problem, it would appear that it has absolutely nothing to do with XML. You simply want a search and replace for "${something}" with "${something-else}", and that is dead easy providing your XML file CANNOT contain any "${...}" strings that you DON'T want replaced.

Have a look at String.replaceAll(), because I think you'll find that it covers everything you need.

And if not...c'mon back

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic