in reply to Regex to ignore comment
So, if I understand correctly:
Assuming you don't have to pay attention to quoting (i.e. pattern declarations that contain quoted #'s), I think the easiest way to accomplish that would be to simply remove any possible comments before parsing a line, i.e.:
while(my $line = <DATA>) { chomp $line ; $line =~ s/\s*#.*$//; # ... }
Here's a few more tips while I'm at it, too.
This bit in the pattern regex:
[\w\d\s\\\/\.\-\@\!\$\%\^\&\*\:\;\,\<\>]
strikes me as an attempt to negate a character class without actually negating it. Are you trying to say "any character other than #" there? If so, you can use [^#] instead.
In the following regex:
/regex\s*=*\s*(.+)|regex\s*=*\s*/
What is the second part of the alternation for? Why not use (.*) in the first part instead? (The same thing applies to the pattern regex, really.)
Speaking of which, you use $1 even though it may not captured anything. Is that what you intended? (And you only use $pattern once; might as well get rid of it.)
Use a while loop instead of a foreach loop; may other monks correct me if I'm wrong, but my understanding is that using foreach will cause all data to be read into memory and then iterated over, which could be an issue depending on how much data you need to handle. while will read your data one line at a time.
I'd also suggest fixing the indenting; don't indent the foreach loop itself, and use consistent indenting inside it (e.g. don't indent the next if statements near the top an extra level).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex to ignore comment
by crusty_collins (Friar) on Oct 20, 2015 at 19:32 UTC | |
by AppleFritter (Vicar) on Oct 20, 2015 at 20:19 UTC | |
by Laurent_R (Canon) on Oct 20, 2015 at 20:17 UTC | |
by Corion (Patriarch) on Oct 20, 2015 at 20:18 UTC |