Completely new to java and I have been playing around with regex in a replaceAll command and wondered if the way that I have done it is the best way?
I basically wanted to find every occurence of
<Letter_File TIMESTAMP="0000-00-00 00:00" FILECREATOR="XXX" BRAND_ID="0" BRAND_NAME="xxxxxxxxx">
within my file and replace it with <Letter_File> i am using the following:
str1 = str1.replaceAll("\\<Letter\\_File[a-zA-Z\\_\\s\\=\\\"0-9-\\:\\\"]+\\>","<Letter_File>");>
what I wanted to know, is this the best way of doing the function or is there a way that the REGEX can be shortened?
Any feedback is more then welcome.
Thanks
-
so basically you are trying to replace the tag <Letter_File TIMESTAMP="0000-00-00 00:00" FILECREATOR="XXX" BRAND_ID="0" BRAND_NAME="xxxxxxxxx"> with <Letter_File> tag in your XML file right ?Karthikeyan Arumugam– Karthikeyan Arumugam2012年03月21日 15:20:40 +00:00Commented Mar 21, 2012 at 15:20
-
yes correct I basically want to strip all of the attributes out of the <Letter_File> tag, I know that this is probably a very noobish way of doing so!Simon.Knott86– Simon.Knott862012年03月22日 09:50:23 +00:00Commented Mar 22, 2012 at 9:50
1 Answer 1
How about:
str1 = str1.replaceAll("<Letter_File[^>]+>","<Letter_File>");>
4 Comments
[^>] means any character that is not >. Have a look at this site: regular-expressions.info/charclass.html ^ means start of string except when inside a character class it negates the class.