regular expressions

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

    #16
    i changed the import (4u) and i still get the highlights

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #17
      Originally posted by oll3i
      i changed the import (4u) and i still get the highlights
      Remove
      Code:
       import com.sun.org.apache.xalan.internal.xsltc.compiler.P attern;
      Just use the standard Java API with import java.util.regex .*;

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #18
        it fixed the prob
        but it doesnt replace a double in a string is this pattern correct for a double "[0-9]{0,2}\\.{0,1}[0-9]{0,2}"; and it reads in every second line not every line i checked it with system.out

        Code:
        private void write_to_file(String wplata_wyplata){
        	try{
        		
        		
        		
        	BufferedReader in = new BufferedReader(new FileReader("konta//"+NumerKonta+".txt"));
            
            
        	   
            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();
            	wczytane_dane+='\n';
            	
          }
          
          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.replaceFirst(replacementStr);
          System.out.println("output ze zmienionym balansem"+output); 
            
            in.close();
        //here will be writing to a file
        	  }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

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #19
          Originally posted by oll3i
          it fixed the prob
          but it doesnt replace a double in a string is this pattern correct for a double "[0-9]{0,2}\\.{0,1}[0-9]{0,2}"; and it reads in every second line not every line i checked it with system.out

          Code:
            while((line = in.readLine()) != null)   {   
          	    wczytane_dane+=line;
              	line = in.readLine();   	
            }
          You have two readlines - that's your 'every-other line' problem

          As far as the regex goes - does the period need to be backwhacked?

          I would think it would be something like [0-9]+.[0-9]+ just quick and rough, that's at least one 0-9, a period (not sure if this needs to be backwhacked or not), and at least one 0-9 digit, but could be any amount.

          The way you have it, you pick up a number between 10 and 99 whose second digit is two or zero, and then three places after that. Is that what you want?

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #20
            thank u guys u r so helpful i dont know how i cd overlook that i had 2 readlines
            ... it finds and replaces the double now
            Last edited by oll3i; Mar 14 '07, 06:22 PM. Reason: typo

            Comment

            Working...