Rule pattern matching with pyparse


  • Anonymous

    I am trying to create a text editor to validate user entries according to a set of rules
    here are some examples

    - Rule 1: get_word "anyword";
    - Rule 2: get_number "0-9";
    - Rule 3: get_words "any number of words";

    So these being my 3 basic rules if a user were to type the following lines of text

    - get_word Dog;
    - get_number 2012;
    - get_word Cat
    - get_word simple editor?
    - get_words The quick brown fox;

    I want to parse each line and return the rule that is the best match
    so the first line get_word Dog; would satisfy Rule 1

    get_number 2012; would satisfy Rule 2

    While get_word Cat would not satisfy any of the 3 rules as it doesn't end in ;(semi-colon)

    Also get_word simple editor? would not satisfy any of the rules as it too doesn't end in a ;
    and it has more than 1 word

    get_words The quick brown fox; would satisfy Rule 3.

    So all I really want is to check each line against the set of rules and get a "Yes that's rule 2" or "Sorry, that doesn't match any rule"

    How can i get this done, any examples would really help

    Note I recently came across pyparsing (Paul McGuire) and I hope this is the right library or am I barking up the wrong tree?