Thread: Regex Pattern Question

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    Regex Pattern Question

    I'm working on a regex parser that sifts through an input file and matches a student's name to their grade.

    Code:
    char name[BUF_SIZE];
    
    sprintf(name,argv[2]);     /*copy netid into name */
    printf("Grade Info For '%s'\n\n",name);
    
    sprintf(name_grade, "(\\-.[char name goes here]).\\|.([0-9]+)");
    How can I get the 2nd sprintf line to regex match on the name stored in char name[] ?

    The file input format is

    Code:
     - studentname | 29
    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    So if the given argument has name "bob", the regex should look like "- bob | ([0-9])+"?

    Why dont you just print it?
    Code:
    sprintf(name_grade, "(\\-.%s).\\|.([0-9]+)", name);
    Or whatever format it is--just use it as if you were "printf"ing.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    Awesome, didn't realize that was an option. Thanks!

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    No problem. Keep in mind that "sprintf" and "fprintf" are the string- and file-output equivalents of screen-output "printf", respectively. So anything you can do in "printf", is just as easily done in the other functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a sed and regex question
    By lehe in forum Linux Programming
    Replies: 1
    Last Post: 09-03-2009, 01:05 PM
  2. Visitor / Decorator Pattern
    By MarkZWEERS in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2009, 11:53 AM
  3. Hmm.. Ai? Finding the pattern in number squences?
    By Zeusbwr in forum C++ Programming
    Replies: 8
    Last Post: 04-02-2005, 06:13 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. (pattern *) pat & pattern * pat
    By siubo in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 10:03 PM