aspose file tools*
The moose likes Beginning Java and the fly likes Need help with Pattern matching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with Pattern matching" Watch "Need help with Pattern matching" New topic
Author

Need help with Pattern matching

kranthi chamarthi
Ranch Hand

Joined: May 08, 2007
Posts: 95
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7784
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11312
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
kranthi chamarthi
Ranch Hand

Joined: May 08, 2007
Posts: 95
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.
Edwin Gonzales
Greenhorn

Joined: Jul 22, 2013
Posts: 12

Use this:



Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7784
    
  21

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Pattern matching