1

The regex pattern is :

enter image description here

However its not a valid pattern as the syntax is wrong (there should be a . preceding * in 2 places)

Correct pattern is :

enter image description here

Is there a way to validate the syntax of these regex patterns in C programming? Any library or function which would evaluate the above wrong pattern and return invalid pattern? I tried using regcomp (but it did not return invalid pattern for the wrong input)

James Z
  • 12,209
  • 10
  • 24
  • 44
Person
  • 11
  • 4
  • regex are not a part of the C language, so you would need to find an external library. – Morten Jensen Feb 01 '19 at 11:21
  • This would be helpful https://stackoverflow.com/a/3643454/10035556 – Mayur Feb 01 '19 at 11:22
  • regcomp did not return invalid pattern for the wrong input which i have provided – Person Feb 01 '19 at 11:28
  • which library can be used to serve the purpose of validation? – Person Feb 01 '19 at 11:28
  • 1
    Please don't include text as images. If anyone wants to copy that, they won't be able to – James Z Feb 01 '19 at 11:38
  • actually i pasted the original text, but few characters were not seen correctly after posting it – Person Feb 01 '19 at 11:40
  • "Is there a way to do X in C"? Yes. – n. m. could be an AI Feb 01 '19 at 11:43
  • PIck a regular expression library. Use it to try to compile the expression, see if that succeeds or not. Note that if your RE is being stored as a string literal, you'll have to escape the backslashes in it so that they're treated as literal characters and not the start of a escape sequence themselves.. – Shawn Feb 01 '19 at 11:45

3 Answers3

0

You might want to go overboard and do it in C with a library, or check out these live testing tools https://regex101.com/ or https://www.debuggex.com/

Gunther Schulz
  • 575
  • 3
  • 18
0

There are regex libraries for c you can use (see Regular expressions in C: examples? ). When you want to find out, if a given string has valid regex format, you can do this by using another regex ( see Is there a regular expression to detect a valid regular expression?), or you can try to "compile" the string as regex. I think the first way is the cleaner way.

jjj
  • 575
  • 1
  • 3
  • 16
0

It depends on the regex implementation you're using. Here's an example using POSIX extended regular expressions that simply checks the return value of regcomp and prints an error message obtained with regerror:

#include <regex.h>
#include <stdio.h>
#include <string.h>

void test(const char *regex) {
	regex_t preg;
	int errcode = regcomp(&preg, regex, REG_EXTENDED);
	if (errcode == 0) {
		printf("%s => Success\n", regex);
		regfree(&preg);
	}
	else {
		char errmsg[80];
		regerror(errcode, NULL, errmsg, 80);
		printf("%s => %s\n", regex, errmsg);
	}
}

int main() {
	test("(*\\.com)");
	test("(.*\\.com)");
	return 0;
}

Try it online!

This should print something like:

(*\.com) => Invalid preceding regular expression
(.*\.com) => Success

Note that (*\.com) is a valid POSIX basic regex since an unescaped ( matches a literal (. With basic regexes, a * at the beginning of a regex or parenthesized subexpression also matches a literal *.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
  • With the same code, if we try the above pattern(what i have mentioned in the question) , it fails to throw error, rather compiles successfully (no . before *) – Person Feb 01 '19 at 16:15