I’m using regex.h (POSIX) for regular expressions. Are there any selection methods for regex matches in C?
I can quite easily check for regular expressions but if I need to retrieve the matched value, I have to manually find and store it.
{{( )*(([[:alnum:]]+\\.)*)?[[:alnum:]]+( )*}}
This regex looks for any variable matches in double curly braces. But I only need the most central item in the string. How can I retrieve the value with regular expressions in C?
You need to pass in a set of
regmatch_ts that the regex can fill with the indices of the matches. Try the below program with a single command line argument (the string to test).Once you have the indices of the matches, it should be fairly easy to pull out what you’re after. (Note:
matches[0]will be the match of the entire expression, so the subexpressions start atmatches[1].)