Home: Perl Programming Help: Regular Expressions:
Regular expression



avalanche
New User

Dec 11, 2012, 6:56 PM


Views: 22882
Regular expression

Hello Experts,

I am new to Perl and I face a problem. Unfortunately time is pressing.

I have a script which reads a line starting with a specific string, and parses the line assigning some strings to variables.

if (/^URI configuration/) {
($A,$B) = /^URI\s+configuration\s+for\s+name\s+(\S+)\,\s+URI\s+(\S+)*/;

It works perfect when $uri_name is a single string. However it is possible that this string in the input contains spaces.
The regexp stops at the first space. Also following the string $uri_name there is a number in inside parenthesis that I do not want to parse.

Example line: URI configuration for name Just_name, URI uri name (1073741909)

I want $A = Just_name
$B = "uri name"

Current Regexp works when $B=uri_name

Thanks!


BillKSmith
Veteran

Dec 11, 2012, 8:35 PM


Views: 22872
Re: [avalanche] Regular expression

A minor fix to your regexpr will fix the problem as stated.
You probably shoud consider the module Regex::Common::URI::http.

Code
use strict; 
use warnings;
my $line = 'URI configuration for name Just_name, URI uri name (1073741909)';
my ($A,$B) = $line =~ /
^URI\s+configuration\s+for\s+name\s+
([^,]+)
\,
\s+URI\s+
([^(]+)*
\s+\(
/x;
print $A, "\n", $B, "\n";

Good Luck,
Bill


avalanche
New User

Dec 11, 2012, 9:23 PM


Views: 22865
Re: [BillKSmith] Regular expression

Thanks so much! It seems to work.


[^something] matches any character except those that [something] denotes; that is, immediately after the leading “[”, the circumflex “^” means “not” applied to all of the rest


Laurent_R
Veteran / Moderator

Dec 11, 2012, 11:17 PM


Views: 22813
Re: [avalanche] Regular expression

At the beginning of a cheracter class, the circumflex '^' means that the class includes any character except those explicitely listed after the circumflex.