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 - extracting data from a file

Status
Not open for further replies.

DJpennywhistle

Programmer
Jun 1, 2000
32
US
I am trying to extract information from a file. I am trying to match the line but I don't understand all the [/\s+ stuff. Is there a place I can get a simple list of what all the functions do.<br><br>What I am actually trying to do is extract the number from a line which looks like<br><br>NET &quot;clk&quot; PERIOD = 9ns ;
 
short answer first.....how to match a number in a string.<br><br>While this stuff can get very concise, for clarity, I will be a little verbose.<br><br><i><br>$string = 'NET &quot;clk&quot; PERIOD = 9ns'; <br>$string =~ /\d+/;&nbsp;&nbsp;&nbsp;&nbsp;# matches the first set of digits found in the string<br>$number = $&;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# $& contains the matched value<br></i><br><br>if you want to be a little more selective about what you match.....<br><i><br>$string = 'NET &quot;clk&quot; PERIOD = 9ns'; <br>$string =~ /NET &quot;clk&quot; PERIOD = (\d+)ns/;&nbsp;&nbsp;&nbsp;&nbsp;# catch the digits in $1<br>$number = $1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# $1 contains the value matched in the first set of paren's.<br></i><br><br>For Perl RegEx stuff <br>The O'Reilly &quot;Programming Perl&quot; book has the functional basics<br>and<br>&nbsp;&quot;Mastering Regular Expressions&quot; by Jeffrey E.F. Friedl,Andy Oram (Editor) also from O'Reilly & Associates goes into excruciating detail.<br><br>.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top