Regular expression substring problem (in C)


Results 1 to 2 of 2

Thread: Regular expression substring problem (in C)

  1. #1
    Join Date
    Aug 2002
    Location
    Essex, UK
    Posts
    937

    Regular expression substring problem (in C)

    I'm afraid I've come across a rather thorny problem involving regular expressions - say I have a regexp compiled using regcomp, and I have a string to match it against. How do I highlight every instance of the regular expression in the string?
    I've got the highlight and unhighlight terminal ANSI escape sequences (which I absolutely have to use, I'm afraid), but I just don't get how to match substrings and then highlight them. Any ideas, or even better, code (I'm stuck, so I'm scraping the bottom of the barrel here...)?
    Registered Linux User #325947

    Check out Feather Linux, my distro.
    (Yes, it's shameless self promotion, deal with it )

  2. #2
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,947
    The manpage for regcomp/regexec (it's the same manpage for both functions) explains it fairly well, if you've used the manpages for C functions before.

    Basically, you call regcomp and DON'T pass it the REG_NOSUB flag. Then, when you call regexec (which does the actual matching), you're supposed to pass in an array of regmatch_t structures, along with the number of structures in the array. regexec will fill in this array with as many matches as it will fit (or as many as exist in the string).

    These structures have a beginning offset / ending offset pair in them, which should tell you where in the string to insert the highlight/unhighlight pairs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •