473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

regular expressions

oll3i
679 512MB
cd anyone help me
i need to find a pattern in a file and replace it with new value
the pattern is a double
Mar 13 '07 #1
19 1739
sicarie
4,677 Expert Mod 4TB
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] .
Mar 13 '07 #2
oll3i
679 512MB
a double i need to find a double in a file and replace it with a new value
how to replace it ?
Mar 13 '07 #3
Ganon11
3,652 Expert 2GB
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.
Mar 13 '07 #4
sicarie
4,677 Expert Mod 4TB
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.
Mar 13 '07 #5
r035198x
13,262 8TB
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.
Mar 14 '07 #6
oll3i
679 512MB
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. private void write_to_file(String wplata_wyplata){
  3.     try{
  4.  
  5.  
  6.  
  7.     BufferedReader in = new BufferedReader(new FileReader(filename));
  8.  
  9.  
  10.  
  11.     String wczytane_dane="";
  12.     String line = in.readLine();
  13.  
  14.  
  15.  
  16.     double sd= sr_dost;
  17.     Double a1 = new Double(sd);
  18.  
  19.   while((line = in.readLine()) != null)   {   
  20.       wczytane_dane+=line;
  21.         line = in.readLine();
  22.  
  23.   }
  24.  
  25.   String patternStr = "[0-9]{0,2}\\.{0,1}[0-9]{0,2}";
  26.   String replacementStr = a1.toString();;
  27.  
  28.  
  29.   Pattern pattern = Pattern.compile(patternStr);
  30.  
  31.  
  32.   Matcher matcher = pattern.matcher(wczytane_dane);
  33.   String output = matcher.replaceAll(replacementStr);
  34.  
  35.  
  36.  
  37. FileWriter fstream = new FileWriter("konta//"+NumerKonta+".txt");
  38. BufferedWriter out = new BufferedWriter(fstream);
  39.  
  40.  
  41. // write wczytane_dane to a file will be here
  42.  
  43.  
  44.  
  45.  
  46. out.close();
  47.  
  48.  
  49.  
  50.  
  51.  
  52.     in.close();
  53.       }catch (FileNotFoundException e) {
  54.             System.err.println("Nie ma takiego konta(File not found!)");
  55.       }catch (IOException e) {
  56.               System.err.println(e);
  57.       } catch (NumberFormatException e) {
  58.              System.out.println("NumberFormatException: " + e.getMessage());    
  59.       }catch (Exception ex)  {
  60.     ex.printStackTrace();  }
  61.  
  62.  
  63. }
  64.  
  65.  
Mar 14 '07 #7
r035198x
13,262 8TB
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. private void write_to_file(String wplata_wyplata){
  3.     try{
  4.  
  5.  
  6.  
  7.     BufferedReader in = new BufferedReader(new FileReader(filename));
  8.  
  9.  
  10.  
  11. String wczytane_dane="";
  12. String line = in.readLine();
  13.  
  14.  
  15.  
  16. double sd= sr_dost;
  17. Double a1 = new Double(sd);
  18.  
  19. while((line = in.readLine()) != null) { 
  20.      wczytane_dane+=line;
  21.     line = in.readLine();
  22.  
  23. }
  24.  
  25. String patternStr = "[0-9]{0,2}\\.{0,1}[0-9]{0,2}";
  26. String replacementStr = a1.toString();;
  27.  
  28.  
  29. Pattern pattern = Pattern.compile(patternStr);
  30.  
  31.  
  32. Matcher matcher = pattern.matcher(wczytane_dane);
  33. String output = matcher.replaceAll(replacementStr);
  34.  
  35.  
  36.  
  37. FileWriter fstream = new FileWriter("konta//"+NumerKonta+".txt");
  38. BufferedWriter out = new BufferedWriter(fstream);
  39.  
  40.  
  41. // write wczytane_dane to a file will be here
  42.  
  43.  
  44.  
  45.  
  46. out.close();
  47.  
  48.  
  49.  
  50.  
  51.  
  52. in.close();
  53.      }catch (FileNotFoundException e) {
  54.          System.err.println("Nie ma takiego konta(File not found!)");
  55.      }catch (IOException e) {
  56.      System.err.println(e);
  57.      } catch (NumberFormatException e) {
  58.      System.out.println("NumberFormatException: " + e.getMessage()); 
  59.      }catch (Exception ex) {
  60. ex.printStackTrace(); }
  61.  
  62.  
  63. }
  64.  
  65.  
Did you import the java.util.regex package?
Mar 14 '07 #8
oll3i
679 512MB
should be
Expand|Select|Wrap|Line Numbers
  1. String output = matcher.replaceFirst(replacementStr); 
  2.  
there
Mar 14 '07 #9
oll3i
679 512MB
i have imported

import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import com.sun.org.apache.xalan.internal.xsltc.compiler.P attern;
Mar 14 '07 #10
r035198x
13,262 8TB
should be
Expand|Select|Wrap|Line Numbers
  1. String output = matcher.replaceFirst(replacementStr); 
  2.  
there
I'm sorry I don't get it. Is it a question or have you got it to work?
Mar 14 '07 #11
oll3i
679 512MB
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.apache.xalan.internal.xsltc.compiler.P attern;

eclipse doesnt say that i need to import some other package
Mar 14 '07 #12
sicarie
4,677 Expert Mod 4TB
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.apache.xalan.internal.xsltc.compiler.P attern;

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?
Mar 14 '07 #13
oll3i
679 512MB
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
The method compile(ClassGenerator, 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.Konto.write_to_file(Konto.java)
at konta_bankowe.Konto.depozyt(Konto.java)
at konta_bankowe.Konto.access$2(Konto.java)
at konta_bankowe.Konto$2.actionPerformed(Konto.java)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Mar 14 '07 #14
sicarie
4,677 Expert Mod 4TB
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?
Mar 14 '07 #15
oll3i
679 512MB
i changed the import (4u) and i still get the highlights
Mar 14 '07 #16
r035198x
13,262 8TB
i changed the import (4u) and i still get the highlights
Remove
Expand|Select|Wrap|Line Numbers
  1.  import com.sun.org.apache.xalan.internal.xsltc.compiler.P attern;
  2.  
Just use the standard Java API with import java.util.regex.*;
Mar 14 '07 #17
oll3i
679 512MB
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

Expand|Select|Wrap|Line Numbers
  1. private void write_to_file(String wplata_wyplata){
  2.     try{
  3.  
  4.  
  5.  
  6.     BufferedReader in = new BufferedReader(new FileReader("konta//"+NumerKonta+".txt"));
  7.  
  8.  
  9.  
  10.     String wczytane_dane="";
  11.     String line = in.readLine();
  12.  
  13.     double sd= sr_dost;
  14.     Double a1 = new Double(sd);
  15.  
  16.   while((line = in.readLine()) != null)   {   
  17.         wczytane_dane+=line;
  18.         line = in.readLine();
  19.         wczytane_dane+='\n';
  20.  
  21.   }
  22.  
  23.   String patternStr = "[0-9]{0,2}\\.{0,1}[0-9]{0,2}";
  24.   String replacementStr = a1.toString();;
  25.  
  26.  
  27.   Pattern pattern = Pattern.compile(patternStr);
  28.  
  29.  
  30.   Matcher matcher = pattern.matcher(wczytane_dane);
  31.   String output = matcher.replaceFirst(replacementStr);
  32.   System.out.println("output ze zmienionym balansem"+output); 
  33.  
  34.     in.close();
  35. //here will be writing to a file
  36.       }catch (FileNotFoundException e) {
  37.             System.err.println("Nie ma takiego konta(File not found!)");
  38.       }catch (IOException e) {
  39.               System.err.println(e);
  40.       } catch (NumberFormatException e) {
  41.              System.out.println("NumberFormatException: " + e.getMessage());    
  42.       }catch (Exception ex)  {
  43.     ex.printStackTrace();  }
  44.  
  45.  
  46. }
  47.  
  48.  
Mar 14 '07 #18
sicarie
4,677 Expert Mod 4TB
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

Expand|Select|Wrap|Line Numbers
  1.   while((line = in.readLine()) != null)   {   
  2.         wczytane_dane+=line;
  3.         line = in.readLine();       
  4.   }
  5.  
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?
Mar 14 '07 #19
oll3i
679 512MB
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
Mar 14 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average:...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.