Help: Match Error

A

Amy Lee

Hello,

Here's my codes to match the following strings:

while (<>)
{
chomp;
if (/\d+\s+dG = (\d+)\s+(.*)/)
{
print "$1\n";
print "$2\n";
}
}

And the string is like this.
97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop

However, when I run my script it outputs nothing.

Could you tell me what's going on?

Thank you very much.

Best Regards,

Amy
 
J

Jürgen Exner

Amy Lee said:
Hello,

Here's my codes to match the following strings:

while (<>)
{
chomp;
if (/\d+\s+dG = (\d+)\s+(.*)/)
{
print "$1\n";
print "$2\n";
}
}

And the string is like this.
97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop

However, when I run my script it outputs nothing.

Could you tell me what's going on?

The minus sign is not a digit.

jue
 
A

Amy Lee

The minus sign is not a digit.

jue
Thanks. And I have noticed that, however if I want to match
dG = 30 and dG = -30, what RE should I take?
Use /dg =( | -)(\d+)/ ?

Amy
 
J

John W. Krahn

Amy said:
Hello,

Here's my codes to match the following strings:

while (<>)
{
chomp;
if (/\d+\s+dG = (\d+)\s+(.*)/)
{
print "$1\n";
print "$2\n";
}
}

And the string is like this.
97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop

However, when I run my script it outputs nothing.

Could you tell me what's going on?

\d matches numerical digits, it doesn't match either '-' or '.'.

Try /\d+\s+dG = (-?[\d.]+)\s+(.*)/ instead.




John
 
A

Amy Lee

Amy said:
Hello,

Here's my codes to match the following strings:

while (<>)
{
chomp;
if (/\d+\s+dG = (\d+)\s+(.*)/)
{
print "$1\n";
print "$2\n";
}
}

And the string is like this.
97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop

However, when I run my script it outputs nothing.

Could you tell me what's going on?

\d matches numerical digits, it doesn't match either '-' or '.'.

Try /\d+\s+dG = (-?[\d.]+)\s+(.*)/ instead.




John
Thank you very much. You show me the way.

Amy
 
M

Martien Verbruggen

Hello,

Here's my codes to match the following strings:

if (/\d+\s+dG = (\d+)\s+(.*)/)

You're looking for

one or more digits,
one or more whitespace characters,
"dG = ",
one or more digits ...
And the string is like this.
97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop

You have here

one or more digits (97)
one or more whitespace characters
"dG = "
a minus....

Ah. That must be why it didn't match. A minus is not a digit. Neither,
BTW is the . in -40.9, so even if you fixed the minus problem, you would
still not get the whole number that you're interested in, I presume. If
all your numbers follow the same format, with optional - only, you could
try

/\d+\s+dG = (-?\d+\.\d+)\s+(.*)/

If there are possible + signs, and if the fractional part is also
optional maybe this will work:

/\d+\s+dG = ([+-]?\d+(?:\.\d+)?)\s+(.*)/

How generic you need to get depends on the format of that number in your
data, and I don't know that. I only have the one example.

Have a look at the entry in perlfaq4 with the title "How do I determine
whether a scalar is a number/whole/integer/float?"

# perldoc -q "determine.*number"

That entry has many regular expressions, one of which you probably can
use, if the sufggestions above aren't sufficient.

Martien
 
B

Ben Morrow

Quoth Amy Lee said:
Thanks. And I have noticed that, however if I want to match
dG = 30 and dG = -30, what RE should I take?
Use /dg =( | -)(\d+)/ ?

That will work. It would be better to use non-capturing brackets:

/dg =(?: | -)(\d+)/

and you don't need the space inside the alternation:

/dg = (?:|-)(\d+)/

and you can just use ? instead:

/dg = -?(\d+)/

Ben
 
T

Tad J McClellan

[ snip how to match a real number ]
Thank you very much. You show me the way.


You needn't be so dependent on others to show you the way.

You will become an efficient Perl programmer if you learn how
to discover "ways" for yourself.

If you have a question about "numbers", you should check the
Frequently Asked Questions that mention them:

perldoc -q number

How do I determine whether a scalar is a number/whole/integer/float?

which has 7 patterns for matching various types of numbers.

Then you would have noticed that many of the patterns have "-" character
in them, and that your pattern did not...

.... problem solved without asking hundreds of other people to do it for you.

Be nice to us.
 

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,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top