regular expressions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    #1

    regular expressions

    cd anyone help me
    i need to find a pattern in a file and replace it with new value
    the pattern is a double
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by oll3i
    cd anyone help me
    i need to find a pattern in a file and replace it with new value
    the pattern is a double
    The word 'double' or just any double?

    If it's any double and you're reading stuff in from a file, you would need to set up a Scanner, and then go line by line looking for [number].[number] .

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      a double i need to find a double in a file and replace it with a new value
      how to replace it ?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        You could read the entire file into a String, find the double and replace it, and then rewrite the original file with the altered String.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by Ganon11
          You could read the entire file into a String, find the double and replace it, and then rewrite the original file with the altered String.
          If you read the line in (and here is where I hope that I remember correctly that were in the Java forum) you can do a Pattern.match() on a pre-compiled regex, or if you wanted to use strings, you can do String.indexOf( ) for the period and test both sides for numbers.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by oll3i
            a double i need to find a double in a file and replace it with a new value
            how to replace it ?
            Text files don't have data types. When you read from a Java program is when you choose which data type you want to read the values as. If you use scanner, you can read as double directly but since you say that you want to replace the double then you should the data as strings because Patterns are strings. Do you know the expression for a decimal number then? You might need to have a look at a regex tutorial first if you don't.

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #7
              why it highlights the Pattern.compile (patternStr); and
              pattern.matcher (wczytane_dane) ; as an error
              also is [0-9]{0,2}\\.{0,1}[0-9]{0,2} good for a double
              Code:
              private void write_to_file(String wplata_wyplata){
              	try{
              		
              		
              		
              	BufferedReader in = new BufferedReader(new FileReader(filename));
                  
                  
              	   
                  String wczytane_dane="";
                  String line = in.readLine();
              
              
              
                  double sd= sr_dost;
                  Double a1 = new Double(sd);
              
                while((line = in.readLine()) != null)   {   
              	  wczytane_dane+=line;
                  	line = in.readLine();
              
                }
              
                String patternStr = "[0-9]{0,2}\\.{0,1}[0-9]{0,2}";
                String replacementStr = a1.toString();;
              
              
                Pattern pattern = Pattern.compile(patternStr);
              
              
                Matcher matcher = pattern.matcher(wczytane_dane);
                String output = matcher.replaceAll(replacementStr);
                
              
                
              FileWriter fstream = new FileWriter("konta//"+NumerKonta+".txt");
              BufferedWriter out = new BufferedWriter(fstream);
              
              
              // write wczytane_dane to a file will be here
              
              
              
              
              out.close();
                  
                  
               
                  
                  
                  in.close();
              	  }catch (FileNotFoundException e) {
              		    System.err.println("Nie ma takiego konta(File not found!)");
              	  }catch (IOException e) {
              	          System.err.println(e);
              	  } catch (NumberFormatException e) {
              	         System.out.println("NumberFormatException: " + e.getMessage());    
              	  }catch (Exception ex)  {
                  ex.printStackTrace();  }
              	
              	
              }

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by oll3i
                why it highlights the Pattern.compile (patternStr); and
                pattern.matcher (wczytane_dane) ; as an error
                also is [0-9]{0,2}\\.{0,1}[0-9]{0,2} good for a double
                Code:
                 
                private void write_to_file(String wplata_wyplata){
                	try{
                 
                 
                 
                	BufferedReader in = new BufferedReader(new FileReader(filename));
                 
                 
                 
                String wczytane_dane="";
                String line = in.readLine();
                 
                 
                 
                double sd= sr_dost;
                Double a1 = new Double(sd);
                 
                while((line = in.readLine()) != null) { 
                	 wczytane_dane+=line;
                	line = in.readLine();
                 
                }
                 
                String patternStr = "[0-9]{0,2}\\.{0,1}[0-9]{0,2}";
                String replacementStr = a1.toString();;
                 
                 
                Pattern pattern = Pattern.compile(patternStr);
                 
                 
                Matcher matcher = pattern.matcher(wczytane_dane);
                String output = matcher.replaceAll(replacementStr);
                 
                 
                 
                FileWriter fstream = new FileWriter("konta//"+NumerKonta+".txt");
                BufferedWriter out = new BufferedWriter(fstream);
                 
                 
                // write wczytane_dane to a file will be here
                 
                 
                 
                 
                out.close();
                 
                 
                 
                 
                 
                in.close();
                	 }catch (FileNotFoundException e) {
                		 System.err.println("Nie ma takiego konta(File not found!)");
                	 }catch (IOException e) {
                	 System.err.println(e);
                	 } catch (NumberFormatException e) {
                	 System.out.println("NumberFormatException: " + e.getMessage()); 
                	 }catch (Exception ex) {
                ex.printStackTrace(); }
                 
                 
                }
                Did you import the java.util.regex package?

                Comment

                • oll3i
                  Contributor
                  • Mar 2007
                  • 679

                  #9
                  should be
                  Code:
                  String output = matcher.replaceFirst(replacementStr);
                  there

                  Comment

                  • oll3i
                    Contributor
                    • Mar 2007
                    • 679

                    #10
                    i have imported

                    import java.util.regex .MatchResult;
                    import java.util.regex .Matcher;
                    import com.sun.org.apa che.xalan.inter nal.xsltc.compi ler.Pattern;

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by oll3i
                      should be
                      Code:
                      String output = matcher.replaceFirst(replacementStr);
                      there
                      I'm sorry I don't get it. Is it a question or have you got it to work?

                      Comment

                      • oll3i
                        Contributor
                        • Mar 2007
                        • 679

                        #12
                        no i havent it still highlights Pattern.compile (patternStr); (highlights the word compile)and pattern.matcher (wczytane_dane) ; (highlights the word matcher)as an error
                        i have imported
                        import java.util.regex .MatchResult;
                        import java.util.regex .Matcher;
                        import com.sun.org.apa che.xalan.inter nal.xsltc.compi ler.Pattern;

                        eclipse doesnt say that i need to import some other package

                        Comment

                        • sicarie
                          Recognized Expert Specialist
                          • Nov 2006
                          • 4677

                          #13
                          Originally posted by oll3i
                          no i havent it still highlights Pattern.compile (patternStr); (highlights the word compile)and pattern.matcher (wczytane_dane) ; (highlights the word matcher)as an error
                          i have imported
                          import java.util.regex .MatchResult;
                          import java.util.regex .Matcher;
                          import com.sun.org.apa che.xalan.inter nal.xsltc.compi ler.Pattern;

                          eclipse doesnt say that i need to import some other package
                          What is the error it is giving you - can you copy and paste it here?

                          Comment

                          • oll3i
                            Contributor
                            • Mar 2007
                            • 679

                            #14
                            Exception in thread "AWT-EventQueue-0" java.lang.Error : Unresolved compilation problems:
                            The method compile(ClassGe nerator, MethodGenerator ) in the type Expression is not applicable for the arguments (String)
                            The method matcher(String) is undefined for the type Pattern

                            at konta_bankowe.K onto.write_to_f ile(Konto.java)
                            at konta_bankowe.K onto.depozyt(Ko nto.java)
                            at konta_bankowe.K onto.access$2(K onto.java)
                            at konta_bankowe.K onto$2.actionPe rformed(Konto.j ava)
                            at javax.swing.Abs tractButton.fir eActionPerforme d(Unknown Source)
                            at javax.swing.Abs tractButton$Han dler.actionPerf ormed(Unknown Source)
                            at javax.swing.Def aultButtonModel .fireActionPerf ormed(Unknown Source)
                            at javax.swing.Def aultButtonModel .setPressed(Unk nown Source)
                            at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Unk nown Source)
                            at java.awt.Compon ent.processMous eEvent(Unknown Source)
                            at javax.swing.JCo mponent.process MouseEvent(Unkn own Source)
                            at java.awt.Compon ent.processEven t(Unknown Source)
                            at java.awt.Contai ner.processEven t(Unknown Source)
                            at java.awt.Compon ent.dispatchEve ntImpl(Unknown Source)
                            at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
                            at java.awt.Compon ent.dispatchEve nt(Unknown Source)
                            at java.awt.Lightw eightDispatcher .retargetMouseE vent(Unknown Source)
                            at java.awt.Lightw eightDispatcher .processMouseEv ent(Unknown Source)
                            at java.awt.Lightw eightDispatcher .dispatchEvent( Unknown Source)
                            at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
                            at java.awt.Window .dispatchEventI mpl(Unknown Source)
                            at java.awt.Compon ent.dispatchEve nt(Unknown Source)
                            at java.awt.EventQ ueue.dispatchEv ent(Unknown Source)
                            at java.awt.EventD ispatchThread.p umpOneEventForF ilters(Unknown Source)
                            at java.awt.EventD ispatchThread.p umpEventsForFil ter(Unknown Source)
                            at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(Unknown Source)
                            at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
                            at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
                            at java.awt.EventD ispatchThread.r un(Unknown Source)

                            Comment

                            • sicarie
                              Recognized Expert Specialist
                              • Nov 2006
                              • 4677

                              #15
                              This probably won't change anything, but can you try real quick for me to change your import to import java.util.regex .*; for me?

                              Comment

                              Working...