Pattern, matcher in Java, REGEX help -
I'm just trying to get rid of the duplicate constant words from a text file, and someone told me something like that I can do this:
Patterns p = Pattern.compile ("(\\ w +) \\ 1"); Strongbilder SB = New string builder (1000); Int i = 0; (String s: lineoffined) {// line glossary is a list & lt; String & gt; In each line, the txt file is read from Matcher m = p.matcher (s.toUpperCase ()); // And then like some time (m.find ()) {// do something here}
I have tried to see if I can make a new string , Or remove the matches where there are matches, but I was not sure how it works after reading the document. For example, how does this work as a test case, I did this:
if (m.find ()) {System.out.println (s.sstring (i, M.end ())); }
This text file contains: This is an example example testing test test.
Why is my output this
?
EDIT:
If I have an ArrialList lineoffax that reads each line from a line of .txt file and then I create a new arrelist to capture the modified String For example
list & lt; String & gt; New String = New Arrestist & lt; String & gt; (); (String S: lineoffads {s = s.replaceAll (code from cabbage here); newString.add (s);}
But then it does not give me new, but the original S. Is it because of shallow vs deep copy?
Try to do something like this:
s = s.replaceAll ("\\ b (\\ w +) \\ b (\\ s + \\ 1 + + \\ b", "$ 1");
This regex is slightly stronger than you - it checks for whole words (no partial matches), and you can not touch any number of frequent iterations. Keara asserts.
regex captures one first word: \ b (\ W +) \ b
, and then attempt to match that word with spaces and iterations: To avoid partial matching of (\ s + \ 1) +
.definal \ b
\ 1
, "for formatting "
.
Comments
Post a Comment