Detecting regular expression in content during parse
Posted
by sonofdelphi
on Stack Overflow
See other posts from Stack Overflow
or by sonofdelphi
Published on 2010-05-26T19:21:32Z
Indexed on
2010/05/26
19:41 UTC
Read the original article
Hit count: 253
I am writing a parser for C. I was just running it with some other language files (for fun, to see the extent C-likeness). It breaks down if the code being parsed contains regular expressions...
Case 1: For example, while parsing the JavaScript code snippet,
var phone="(304)434-5454"
phone=phone.replace(/[\(\)-]/g, "")
//Returns "3044345454" (removes "(", ")", and "-")
The '(', '[' etc get matched as starters of new scopes, which may never be closed.
Case 2: And, for the Perl code snippet,
# Replace backslashes with two forward slashes
# Any character can be used to delimit the regex
$FILE_PATH =~ s@\\@//@g;
The // gets matched as a comment...
How can I detect a regular expression within the content text of a "C-like" program-file?
© Stack Overflow or respective owner