-2

I am novice to C programming. Is it possible to match pattern inside the C string, like any built in functions?

I am using Red Hat Linux and I want to check if a string starts with abc: or def: followed by 10 digit numbers @ chars. Something like : (abc|def):([0-9]{10})@([A-Za-z0-9]*).

Is there any C built in function which I can use to check this pattern matching.

Thanks for your help.

Paul Rooney
  • 19,499
  • 9
  • 39
  • 60
Anita
  • 1
  • 3

2 Answers2

1

You can use POSIX regex matching in linux. See man 3 regex for more details. If you are looking for a fast, safe and threaded library you can use re2 library from Google with support like pre-compiled regex. (https://github.com/google/re2)

Rajat Paliwal
  • 604
  • 7
  • 11
0

Linux and POSIX have regex(3) functions. You'll need to "compile" -that is prepare by transforming- your regular expression string using regcomp first.

In some simple cases, you might also use other string functions, like strstr, strchr, sscanf(3) (the %n format specifier might be handy, to know how much bytes you have scanned).

You could also consider some lexing utility, perhaps using flex to generate a tokenizer able to recognize your regexp.

rici
  • 219,119
  • 25
  • 219
  • 314
Basile Starynkevitch
  • 216,767
  • 17
  • 275
  • 509