ANSI C: How to validate input string using regular expression?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mattmao
    New Member
    • Aug 2007
    • 121

    ANSI C: How to validate input string using regular expression?

    Hello everyone.

    This question is regarding ISBN number checking. I've done it with my own algorithm, which is pretty bad in consideration about the total lines of code.

    Now I want to improve it, I guess the best practice wold be using regular expression to handle this string validation:

    Say, I've got the user input string with fgets(array, 20, stdin);

    If it is a valid ISBN number, then it must have a valid format:
    - the valid char set is:"0123456789 X- \n"
    - there must be exactly 10 "digit" chars, which are in this set "0123456789 X"
    - char 'X' must either not exist or at the last position of the digits.
    ...

    It's about the content checking, I don't know how to apply the logic into the code.

    Thanks in advance!




    Sincerely yours,

    Matt
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by mattmao
    Hello everyone.

    This question is regarding ISBN number checking. I've done it with my own algorithm, which is pretty bad in consideration about the total lines of code.

    Now I want to improve it, I guess the best practice wold be using regular expression to handle this string validation:

    Say, I've got the user input string with fgets(array, 20, stdin);

    If it is a valid ISBN number, then it must have a valid format:
    - the valid char set is:"0123456789 X- \n"
    - there must be exactly 10 "digit" chars, which are in this set "0123456789 X"
    - char 'X' must either not exist or at the last position of the digits.
    ...

    It's about the content checking, I don't know how to apply the logic into the code.

    Thanks in advance!




    Sincerely yours,

    Matt


    Hi,
    I cant get what you mean by "cant apply the logic to your code"
    R u facing any issues with the logic or with coding?
    Please be more specific abt the help u need and we can help you out

    Thanks
    Raghuram

    Comment

    • mattmao
      New Member
      • Aug 2007
      • 121

      #3
      Sorry for that.

      Here are my puzzles:

      First, it seems that the ANSI C doesn't have a regular expression lib. I've checked with all the .h file lists...

      Second, say if I get the regular expression exp=[0-9]{10}; how to use it to check against the user input string?

      I can solve this kind of problem using JavaScript because JavaScript can use regular expression to validate the input string, but I don't know how to do that in ANSI C.




      Sincerely yours,

      Matt

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        There is no regular expression facility available in the ANSI standard library but
        there are several libraries available that do implement this facility; google for
        'regular expression ANSI C' and see which one you like most.

        If you can't find anything you might google for 'Henry Spencer regular expression'.
        Henry is one of the regular expression gurus and largely defined what POSIX
        compliant regexes should look like.

        kind regards,

        Jos

        Comment

        Working...