Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REGULAR EXPRESSIONS - easy for a master

Status
Not open for further replies.

DJpennywhistle

Programmer
Jun 1, 2000
32
US
I need to get the two numbers from this line immediatley followed by ns (ie the 5.678 & 7.908)<br><br>DAMN &quot;I_DONT_KNOW_PERL&quot; WELL = 23 nS AS ¦ 5.678ns&nbsp;&nbsp;&nbsp;¦&nbsp;&nbsp;&nbsp;7.908ns&nbsp;&nbsp;&nbsp;&nbsp;¦&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7<br><br>Cheers<br><br>(sorry if this is a bit simplistic - I've only been doing perl for 3 days) <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
<FONT FACE=monospace>#!/usr/local/bin/perl<br>$string = 'stuff ¦ 12.34ns ¦ 34.56ns';<br><br># explain match - <font color=red>\d*</font> - matches zero or more digits<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <font color=red>\.{0,1}</font> - matches zero or one dot<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <font color=red>\d*</font> - matches zero or more digits<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <font color=red>ns</font> - obviously matches 'ns'<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- the <font color=red>'g'</font> - match as many times as possible<br>while ($string =~ /\d*\.{0,1}\d*ns/g) <br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$&\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br># <font color=red> $& </font> catches matched patterns<br></font>
 
Take a look at the split function.&nbsp;&nbsp;Say your string is in the variable $My_String...<br><FONT FACE=monospace><br>@Items = split(&quot;¦&quot;, $My_String);<br></font><br>@Items is an array that will now hold each field from $My_String as a seperate array item.&nbsp;&nbsp;The &quot;¦&quot; in the split command says to use the ¦ character as a field delimiter.<br><br>Remembering that arrays in perl are indexed from 0, you can now refer to the &quot;ns&quot; fields as <FONT FACE=monospace>$Items[1]</font> and <FONT FACE=monospace>$Items[2]</font>.<br><br>Hope this helps.
 
How do I make the string $My_String - it is in a complete file of data of various sorts. <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top