Perl help needed

S

Saya

Hi,

I am a newbie to perl and regular expressions.
I need help with extracting a substring.
Presume:
$test = "thisIsATest(12)";
$subPar = should equal to (12)
$num = should equal to 12

How do I achieve this in perl ? Do I use regular expression ?

Any help would be appreciated.
 
J

Janek Schleicher

Saya wrote at Wed, 02 Jul 2003 06:44:10 -0700:
I am a newbie to perl and regular expressions.

The language is called Perl.
Please read
perldoc -q 'difference between "Perl" and "perl"'
I need help with extracting a substring.
Presume:
$test = "thisIsATest(12)";
$subPar = should equal to (12)
$num = should equal to 12

Do you mean you want everything between brackets in $subPar and
the interior content of them in $num.
That question seems to be stupid, but it's important to have an exacat
definition of the possible input and the wanted output.
What happens if there are more than one bracket? (e.g. "test (1) (2) (3)")
What when there are also not-numbers inside? (e.g. "test (12 pound)")
How do I achieve this in perl ? Do I use regular expression ?

Any help would be appreciated.

What have you tried so far?
A solution would be

my ($subPar, $num) = $test =~ /( \( (.*) \) )/x;

There might be some problems depending on the exact definition.

However, you should really read at least a regexp tutorial, like
perldoc perlrequick
perldoc perlretut
perldoc perlre


Greetings,
Janek
 
J

John W. Krahn

Saya said:
I am a newbie to perl and regular expressions.
I need help with extracting a substring.
Presume:
$test = "thisIsATest(12)";
$subPar = should equal to (12)
$num = should equal to 12

How do I achieve this in perl ? Do I use regular expression ?

$ perl -le'
$test = "thisIsATest(12)";
( $subPar, $num ) = $test =~ /(\((\d+)\))/;
print $subPar;
print $num;
'
(12)
12



John
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top