Results 1 to 6 of 6

Thread: Regex to extract lines

  1. #1
    Join Date
    Dec 2009
    Posts
    202

    Regex to extract lines

    Hello,
    I have some ideas in regular expressions but here I can not . I have a text file which I would get the lines starting with either FX. Y where X is any number (which can begin with zero) and Y a space or by . XY with X and Y have the same definition as above.
    Here are the piece of code that is the problem:

    Code:
    while((ln = in.readLine())!=null){
                                if(Pattern.games("F [0-9] +\\."ln) | | Pattern.games("\\.[0-9]+"ln)){
                                    out.write(+ Onln System.getProperty("ln.separator"));
                                }
    With this code, all output files are empty. Thank you in advance for your help

  2. #2
    Join Date
    Nov 2009
    Posts
    335

    Re: Regex to extract lines

    Hello,
    I see no space in your regular expressions, perhaps your problem came from there.
    Note: You can replace [0-9] by \ d
    by the following line in your code.
    Code:
    if(Pattern.games(F\\d +\\. "Line) | | Pattern.games("\\.\\d + "Line))
    First replace this line in your code and then see if it works or not and if you are getting any error then post back with the error.

  3. #3
    Join Date
    Dec 2009
    Posts
    211

    Re: Regex to extract lines

    Hello,
    Eve I had a similar kind of a problem, so i am interested in this thread, the solution you mentioned, i have tried it
    Code:
    Note: You can replace [0-9] by \ d
    Code:
    
    if(Pattern.games(F\\d +\\. "Line) | | Pattern.games("\\.\\d + "Line))
    This line of the code does not work, thought thanks for the help.
    I have tried something else here, just have a look
    Code:
    while((ln = in.readLine())!=null){
                                if(Pattern.games("^ F\\d +\\. "ln) | | Pattern.games("^\\.\\d + "ln)){
                                    out.write(+ Onln System.getProperty("ln.separator"));
                                }
                            }
    Any more help on this is highly appreciated. Thanks in advance.

  4. #4
    Join Date
    Nov 2009
    Posts
    446

    Re: Regex to extract lines

    Hello,
    In fact I just realize that you forgot to add ".*" at the end of the regular expression . Indeed, it was defined early in our regular expressions, not the full lines.
    Code:
    if(Pattern.games(F\\d +\\. .*"Line) | | Pattern.games("\\.\\d + .* "Line))
    Just think once before you include this in your code.
    F00.2 * atypical or mixed (G30.8 +)
    and
    Quote:
    F00.9 * unspecified (G30.9 +)
    correpondent does not pattern.

  5. #5
    Join Date
    Dec 2009
    Posts
    202

    Re: Regex to extract lines

    Hello,
    Here is my updated code, have a look at it and if you find any corrections in that to be made, then you can guide me.
    Code:
    public class Extraction {
        
        / ** Creates a new instance of extraction * /
        Public Extraction() {
        }
        Public static void hand(String[] args) {
            String ln;
            
            
            BufferedReader in;
            BufferedWriter out;
            for(int i =1; i <=10; i + +){
                try{
                    in =new BufferedReader(new FileReader(". / sources / sourcei + +. txt "));
                    try{
                        out =new BufferedWriter(new FileWriter(". / output / exiti + +. txt "));
                        try{
                            while((ln = in.readLine())!=null){
                                if(Pattern.games("^ F\\d +\\. .*"ln) | | Pattern.games("^\\.\\d + .* "ln) | | Pattern.games("^\\.\\d +\\* .*"ln)){
                                    out.write(+ Onln System.getProperty("ln.separator"));
                                }
                            }
                            System.out.System.out.println("File"i + +"Done!");
                        } finally{
                            out.close();
                        }
                    }finally{
                        in.close();
                    }
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
            
        }
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Regex to extract lines

    Hello,
    No, the problem is with the regular expression, in fact it is specifically that X can be a decimal number (see your example) which was not included in the regular expressions given so far.
    For example, first it gives something like
    Code:
    "^ F\\d + (\\.\\d +)? .* "
    I hope you are understanding the thing here. If you have any more queries to ask in this then you can post here.

Similar Threads

  1. Substring with a regex
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 02-04-2010, 11:06 AM
  2. How to extract text lines between two strings
    By PinkShell in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 08:10 PM
  3. Regex with xml tag
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 02:11 AM
  4. Query in php regex
    By Linux-Us in forum Software Development
    Replies: 5
    Last Post: 14-12-2009, 12:54 PM
  5. Regex matches last index
    By Mindstorm in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 06:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,497,428.07835 seconds with 16 queries