3

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

2
  • 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 ? Commented 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! Commented Mar 22, 2012 at 9:50

1 Answer 1

3

How about:

str1 = str1.replaceAll("<Letter_File[^>]+>","<Letter_File>");>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks this works fine, just out of curiousity could you explain how this works? am I right in assuming that the [^>] will look for anything after the <Letter_File upto the >? I thought that you had to put in the certain characters it looked for hence why I put all of those different variations e.g. a-zA-Z0-9 etc. etc.
@Simon.Knott86: [^>] means any character that is not >. Have a look at this site: regular-expressions.info/charclass.html
thanks very much I think that helps alot, I wasnt sure as the ^ character to my understanding can also mean "at the start of"
@Simon.Knott86: Yes the ^ means start of string except when inside a character class it negates the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.