• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with Pattern matching

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
PHP Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use this:



 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic