Results 1 to 6 of 6

Thread: Problem in pattern matching

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Problem in pattern matching

    Hello,
    I manage to write a pattern matching that game with a "rule of my text file that is under cete form:
    Code:
    rule "rrrr"
    	xxxx
    when
    yyyy
                    yyyy
    then
    bbbb
                    bbbb
    end
    so that I can do it for each of these rules, get the value of rrrr, xxxx, my "aaaa" and "bbbb" and can reflects carriage returns which can be as linux or windows and tabs as possible in my example. But it is giving me an error, do you guys have any solution for this. I will he very happy if an sample code is provided.

  2. #2
    Join Date
    Nov 2009
    Posts
    335

    Re: Problem in pattern matching

    Hello,
    The simplest of use can be \ that includes both spaces, tabs that returns a different line. So you can get away with something like:
    Code:
    rule \ s +"(.*?)"\ s +(.*?)\ s + when \ s +((?:.*? \ s +)*?)then \ s +((?:.*? \ s +)*?)end
    Or in the more readable format
    Code:
    (? x)
    rule \ s + "(.*?)" \ s + 
        (.*?) \ s +
        when \ s +
            ( (?: .*? \ s + )*? )
        then \ s +
            ( (?: .*? \ s + )*? )
    end

  3. #3
    Join Date
    Dec 2009
    Posts
    296

    Re: Problem in pattern matching

    Hello,
    Thanks for your reply. When I put this line here, I have a compilation error:
    Code:
    Pattern p = Pattern.compiles("rule\ s+"(.*?)"\ s+(.*?)\ s+ when\ s+((?:.*?\ s+)*?) then\ s+((?:.*?\ s+)*?) end ");
    Otherwise, compared to the solution
    Code:
    \ s + is a multi-whitespace characters.
    However, I still have text between my "rule" and my first carriage return, then,is that this expression is correct? Thank you , do you have any solution for this?

  4. #4
    Join Date
    Nov 2009
    Posts
    583

    Re: Problem in pattern matching

    Hello,
    For the compilation error, because it takes double the \, a \ simple introduces a java escape character, and for a \ character must overtake: \ \. For the text between rule and the first carriage return, it seems that the proposal Uther reflects this with rule \ +"(.*?)" s \ s. To check if you have not already used it somewhere in your code.

  5. #5
    Join Date
    Dec 2009
    Posts
    296

    Re: Problem in pattern matching

    Hello,
    I begin to understand. In fact, my pattern includes a readLine (), so that I read line by line (I think the readLine makes the line without the newline), so it could not walk. I'll put the contents of my entire file into a string, I'm testing again, and I know you want. Thank you and if you have an other alternative then please help me by posting it.

  6. #6
    Join Date
    Nov 2009
    Posts
    343

    Re: Problem in pattern matching

    Hello,
    I have an code with me but not sure that is what you want, you can check it and if it works well and good.
    Code:
    public static void initializeRules(){
            String ln;
            FileInputStream fis = null;
            String flnm =
                "C: / projects / ........../  CV.drl";
            File fl = new File(flnm);
     
            when \ \ +((?:.*? s \ \ s +) *?) then \ \ s +((?:.*? \ \ s +)*?) end ");
            / / Pattern p = Pattern.compile ( "rule \ "(.*) \" \ \ s * ");
            Pattern p = Pattern.compiles("^" rule, Pattern.MULTILINE);
            try {
                String nmrl;
                String wrl;
                String trul;
                String fl2 = FileUtils.readUTF8File(fl);
                System.out.System.out.println("fl2:"+ File);
                Matcher m = p.matcher(fl2);
                if (m.games()){
                    nmrl = m.group(1);
                    / / wrl = m.group (2);
                    / / trul = m.group (3);
                    / / System.out.println ( "Found:" + ln);
                    System.out.System.out.println("nmrl"+ nmrl);
                    / / System.out.println ( "wrl:" + wrl);
                    / / System.out.println ( "trul:" + trul);
                }
                else{
                    System.out.System.out.println("It does not matche");
                }
            } catch (FileNotFoundException e) {
                / / Log the error
            } catch (UnsupportedEncodingException e) {
                / / Log the error
            } catch (IOException e) {
                / / Log the error
            }
        }

Similar Threads

  1. String matching algorithm
    By Kaustubh m in forum Software Development
    Replies: 4
    Last Post: 21-07-2010, 10:10 AM
  2. Problem in the split pattern
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 05:55 AM
  3. Problem in matching Regex
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 04:12 AM
  4. What are matching mutator methods?
    By Abhay singh in forum Software Development
    Replies: 3
    Last Post: 04-11-2009, 03:06 PM
  5. JAVA Help - Background change by color matching
    By Daren in forum Software Development
    Replies: 4
    Last Post: 12-05-2009, 08:41 AM

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,713,965,332.97524 seconds with 17 queries