Originally posted by Piet Verdriet:
: p
Okay, you never gave an exact example in AND ouput, so I might be wrong, but if I understand you crrectly, you want to replace sub strings like this:
<c: param name="AAA" value="<%= aaa %>" />
into:
<c: param name="AAA" value="aaa" />
correct?
If so, you don't need to create any Matcher: replaceAll(...) on a String is sufficient.
Try this:
>> please explain ".*?" and ".+?", that sounds contradictory inasmuch as that sounds like "0 or more characters and 0 or 1 character" along with "1 or more characters and 0 or 1 character"; that does not make sense to me.
>> Thanks for the explanation on ?s and ?m
The (?s) and (?m) flags in front of the regex, tell replaceAll(...) to let the DOT match any character and matches can span over multiple lines.
[ August 27, 2008: Message edited by: Piet Verdriet ]
Originally posted by Mark Vedder:
{Note: I was typing up my reply as Steve posted his and therefore did not see it until I was done. So there's a little bit of redundancy. Great minds think alike and all that... }
That has nothing to do with using an escaping a character in the replacement string. That has to do with the regex itself. If you put a logging statement in, you will see that the find method is only finding the last line. You need to
indicate your pattern is multiline:
That will at least find each pattern that occurs on a separate line. But it will still identify this line:
all as a single match and not two matches.
As such
$1 = <c:param name="myName" value="<%= myValue %>" /> <c:param name="foo" value=" $2 = bar <-- with a trailing space $3 = " />
So the end of the first desired match and the start of the second desired match is matching the regex for the first capturing group, namely the .+
If we change that to a [^>]+ then we will only capture the desired start, but we'll also need to modify the third capturing group:
$1 = <c:param name="myName" value=" $2 = myValue <-- with a trailing space $3 = " /> <c:param name="foo" value="<%= bar %>" />
Now, the .* in the third group is capturing the second desired match. So we can change that to ("[^>]+> ;) and everything will work. Also, the [ \t]* should probably be changed to [\s]* since a carriage return in the middle of an XML element is legal as well as a space or tab.
Also, the regex does not take into account single quoted attribute values. (value = 'myValue'). So we want to change teh " to an option of ' or ".
So adding all our changes together we get:
That will do more along the lines of what you want. May not be perfect, I'd do some unit testing on it to check for other missed options.
There's a great tool available to help with writing regular expressions. It's called RegexBuddy. It does a lot. One of the nicest features is the ability to highlight not only the match, but what a capturing group is matching. It's a commercial product, but well worth the cost as it will save you hours of work.
[ August 25, 2008: Message edited by: Mark Vedder ]
Originally posted by Craig Wood:
Is image cropping even possible in Java?
Yes.
About the exception ClassCastException: sun.awt.image.ToolkitImage cannot be cast to
java.awt.image.BufferedImage:
You probably loaded your source image with one of the older Toolkit getImage or
createImage methods. These return type Image which cannot be cast to BufferedImage.
To obtain a BufferedImage with an Image you can create a new BufferedImage and draw the
Image into it:
Or, you could use a newer image–loading method that returns a BufferedImage:
About the exception RasterFormatException: (x + width) is outside of Raster:
This exception is thrown by the getSubimage method as stated in the BufferedImage
api. You have to be very careful/precise in using this method to avoid trouble. Another
problem with using it to obtain smaller images is that the two images are not independent.
Making changes to one image automatically changes the other. Sometimes this can cause
trouble. You can always copy the subimage into a new BufferedImage.
Another option for obtaining a clipped image is to create a new BufferedImage the size of
your clipping Rectangle, translate its graphics context by clipX, clipY and draw the src
image as above. It takes a little playing–around with to see how it works.