Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public class StringUtilitiesFromFile  {
	public static void main(String[] args) throws IOException 
        {
              File file = new File("D://French/FrenchWords/Auth.html");
		String string = FileUtils.readFileToString(file);  
                String And = string.replaceAll("\\(.*\\)", "");  
		System.out.println(And);    
                System.out.println("");           
       }
}


the result is:
avenue
beard
beneath
boat
bound
bright bronchitis budget builder bulkhead businessman businessperson businesswoman butcher
cage carbon carbon
carcinogen card caution cautious
cave
characteristic
chief
chief clay
clean coach
coal
comfortable
consumer consumer goods
contact
cool courage
courage courageous course court cousin cover cruel
debate defend defendant defense defense defiance deficit
define

which are about twenty four lines but the actual file contains about four hundred lines

what the code does is reads from file and rcmoves anyword with the curly brackets
and the brackets

What I have tried:

changed regular espression and other code types
Posted
Updated 21-Jun-19 21:56pm
v2

Quote:
which are about twenty four lines but the actual file contains about four hundred lines

You forgot to show us what is the input file.
First thing: check what was read from file.
Java
public class StringUtilitiesFromFile  {
	public static void main(String[] args) throws IOException 
        {
              File file = new File("D://French/FrenchWords/Auth.html");
              String string = FileUtils.readFileToString(file);  
              System.out.println(string); 
              System.out.println("------------------------------------------");
              String And = string.replaceAll("\\(.*\\)", "");  
              System.out.println(And);    
              System.out.println("");           
       }
}

[Update]
Quote:
the contents of the file are here, have pasted about thirty lines actual file is much longer

Then you know file read is ok, now change the RegEx replace with RegEx match, and print a match per line.
 
Share this answer
 
v3
Comments
four systems 21-Jun-19 3:25am    
the contents of the file are here, have pasted about thirty lines actual file is much longer it should give you a pretty good idea about what is being done, java reads the file selects the words and write to another file, the words with curly braces are not allowed

the above code does that but only for few selective file and does not iterates throught the whole file


ability (noun) able (adjective) about (preposition) about (adverb) above (adverb) above (preposition) abroad (adverb) absence (noun) absent (adjective) absolute (adjective) abstract abstract (adjective) abuse (verb) abuse (noun) abusive academic (adjective) accept (verb) acceptable (adjective) acceptance access (verb) access (noun)accompany (verb) according+to (preposition) account (verb) account (noun) accountant accurate (adjective)a (indefinite article) abandon (verb) ability (noun) able (adjective) about (preposition) about (adverb) above (adverb) above (preposition) abroad (adverb) absence (noun) absent (adjective) absolute (adjective) abstract abstract (adjective) abuse (verb) abuse (noun) abusive academic (adjective) accept (verb) acceptable (adjective) acceptance access (verb) access (noun) accompany (verb) according+to (preposition) account (verb) account (noun) accountant accurate (adjective)accuse ache (noun) ache achieve (verb) acquire (verb) acquit across (adverb) across (preposition) act (verb) act (noun) action (noun) active activity (noun) actor actress actual (adjective) actually (adverb) adapt (verb) add (verb) addition (noun) additional (adjective) address (noun) address (verb) adequate (adjective) adjourn adjust (verb) administration (noun) admiration admire (verb) admission (noun)admit (verb) adopt (verb) adoption adult (noun) adult (adjective) advance (noun) advance (verb) advantage (noun) adventure (noun) adventurous advertise (verb) advertisement (noun) advice advise (verb) affair (noun) affect (verb) affection affectionate afford (verb) afraid (adjective) after (adverb) after (conjunction) after (preposition) afternoon again (adverb) against (preposition) age (noun) age (verb) agency (noun) agent (noun)aggravation aggressive ago agree (verb) agreement (noun) agriculture ahead (adverb) aid (verb) aid (noun) aim (noun) aim (verb) air air traffic controller airplane (noun) airport (noun) aisle alarm (verb) alarm (noun) alert (noun) alert (adjective) alert (verb) alibi alimony alive (adjective) all (adverb) all (pronoun) all (adjective) all right (adverb) all right (adjective)
four systems 21-Jun-19 12:37pm    
did you mean change String And = string.replaceAll("\\(.*\\)", ""); for something else
Patrice T 21-Jun-19 13:06pm    
You are using replaceAll, but you are blind, you don't know what is going on inside.
See documentation of the match RegEx function, it will allow you to see what are the string matched by your RegEx.
four systems 22-Jun-19 10:14am    
thancs
I guess the problem is your greedy approach :-)
Change the pattern from
Quote:
"\\(.*\\)"

to
Java
"\\(.*?\\)"


The following program works for me
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Braces  {
  public static void main(String[] args) throws IOException
        {
              String input = new String( Files.readAllBytes(Paths.get("Auth.html")));
              System.out.println(input);
              System.out.println("------------------------------------------");
              String output = input.replaceAll("\\(.*?\\)", "");
              System.out.println(output);
              System.out.println("");
       }
}
 
Share this answer
 
Comments
Richard MacCutchan 22-Jun-19 5:04am    
+5. Something new I have learned today.
CPallini 22-Jun-19 5:17am    
Thank you, Richard!
four systems 22-Jun-19 10:15am    
thancs
CPallini 22-Jun-19 11:56am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900