regular expression meaning

N

nicolas-laurent

Could you explain to me what this regular expression means

=~ m,([^;]+);([^\n]+)\n?,g) {


sub build_rules {
my $src = get_file_content($rules_spec);
my %rules;
while ($src =~ m,([^;]+);([^\n]+)\n?,g) {
$rules{$1} = $2;
}
return \%rules
}
 
G

Gunnar Hjalmarsson

nicolas-laurent said:
Could you explain to me what this regular expression means

=~ m,([^;]+);([^\n]+)\n?,g) {


sub build_rules {
my $src = get_file_content($rules_spec);
my %rules;
while ($src =~ m,([^;]+);([^\n]+)\n?,g) {
$rules{$1} = $2;
}
return \%rules
}

The function parses a string like "rule1;abc\nrule2;def\nrule3;ghi" and
assigns the rule/value pairs to a hash.

perldoc perlrequick
perldoc perlretut
perldoc perlre
 
T

Tad McClellan

nicolas-laurent said:
Could you explain to me what this regular expression means

=~ m,([^;]+);([^\n]+)\n?,g) {
^^ ^^^
^^ ^^^

The underlined parts are NOT part of a regular expression.


m,([^;]+) # any chars that are not semicolon
; # required semicolon
([^\n]+) # any chars that are not newline
\n? # optional newline
,gx


ie. Grab 2 chunks of text separated by a semicolon.

It seems a hokey regexification of a vanilla split():

split /;/, $somevar, 2;


Where did you come across that code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top