• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

find replace in file with regular expression

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me, I have been in this mess since long now. I have 2 files, one from which i pick up text and the other which i span to replace the text in.

i was tryin it with regular expressions but i failed without any error messages. please help. Creating scenario below:

I have a variable calculated substrVersion inside a loop while reading 1st file.
Inside the same, I create a second loop for 2nd file, reading it if it finds any occurance of "FileVersion" and I try to replace it with regular expression:

CharSequence inputStr = "codebase='formbuilderocx.cab#version=4,2,0,0' viewastext id='formbuilderocx' >"; // "a b c a b c";

String patternStr = "[0-9][0-9]+(,[0-9][0-9]){3}"; //"a";
String replacementStr = "3,7,0,5";

// Compile regular expression
Pattern pattern = Pattern.compile(patternStr);

// Replace all occurrences of pattern in input
Matcher matcher = pattern.matcher(inputStr);
String output = matcher.replaceAll(replacementStr);

BUT I donot get any updated output string.
Then i tried it the other way round dealing with indexes and appending substrings into an updated new string.

BUT now i cannot replace the string in 2nd file with the new one. When i update 1 line , the other which passes the condition (but was updated in last loop round) is overwritten with old text.

plesae help, i dont know how to embedd mrakers in the file so that tyhe text whichis updated do not get over written when i run the readline again for 2nd updation...

 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Priya Singh:

If you want patternStr to match "4.2.0.0", your pattern is incorrect. Note that "[0-9][0-9]+" matches two or more digits and "[0-9][0-9]" two digits. It would match "382,84,49,21".
 
Priya Singh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to match 3,1,34,1 type of a string .. what expression should i use ?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Priya Singh:
i want to match 3,1,34,1 type of a string .. what expression should i use ?

You need to be more specific than that when using regular expressions. From your example, I'm guessing that you want to find "four one-or-more-digit numbers separated by commas." If that's not correct, please clarify.
 
Priya Singh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes , thats correct.

its 4 numbers seperated by comma and they can be one or two digits.

i tried this too but still i dont get any results ...
str2 = "codebase='formbuilderocx.cab#version=4,2,0,0' viewastext id="formbuilderocx" > ";
CharSequence inputStr = str2 ;
String patternStr = "[0-9]+(,[0-9]){3}";

Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);


StringBuffer buf = new StringBuffer();
boolean found = false;
while ((found = matcher.find())) {

String replaceStr = matcher.group();


replaceStr = str;
matcher.appendReplacement(buf, replaceStr);
}
matcher.appendTail(buf);

String result = buf.toString();
System.out.println("result" + result);
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The pattern you have should match "4,2,0,0", though note that you said "one or two digits" yet your pattern will match one or more digit for the first number and only one digit for the other three numbers. Againk this should match your input.

Maybe you could simplify the expression (though Java does support groups and iteration) with this:That will match four one or two digit numbers separated by commas. If that doesn't work, simplify even more:If that fails, something is wrong with your code.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic