wood burning stoves 2.0*
The moose likes Perl and the fly likes perl pattern matching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Languages » Perl
Bookmark "perl pattern matching" Watch "perl pattern matching" New topic
Author

perl pattern matching

anandkumar biyani
Greenhorn

Joined: Dec 06, 2011
Posts: 9
Hello everyone,

Can anyone help me out how to extract the string after matching the pattern.

Ex:I have a file containing line as below
Number of rows read = 730

I want to extract the value 730.


Thanks in advance.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11144
    
  16

your specs are too vague.

do you want what is after the '='? do you want what is after "Number of rows read = " (and do the spaces after the '=' make a difference?) do you want the numeric part? What if the file contains the line "Number of files that contain 5 elements = 730"?

Giving one example of what you want to do does NOT make a spec. And how you would write this would entirely depend on what, EXACTLY, you want to do. So give us the details and we can help you.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
anandkumar biyani
Greenhorn

Joined: Dec 06, 2011
Posts: 9
Hiii,

Actually i want to match for the pattern Number of rows read and extract the value 730.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

You need a pattern that matches against the whole expression, and that contains a capturing group - a bit in brackets - that is the bit you want to extract. Then, once you've matched it, you can use the variables $0, $1, $2 etc. $0 is the whole string matches, $1 is the first sub-group, etc.

A simple example: if I know a string is a date in the format 2011-07-19, then I can extract the parts like this:
(N.B. not checked - sorry if I got the syntax wrong somewhere).

So that's the mechanism - you just need the right expression. Exactly what that is depends on the exact rules that apply in your situation.
anandkumar biyani
Greenhorn

Joined: Dec 06, 2011
Posts: 9
i have a file which read like this

Number of rows read = 730
Number of rows inserted = 730
Number of rows rejected = 730
Number of rows comitted =730

I want to verify if number of rows read and committed are same. Hence i need to extract those values and compare.
Hence i want to extract those numbers and compare.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11144
    
  16

So you have the string and you need to extract the numeric part?

 
Don't get me started about those stupid light bulbs.
 
subject: perl pattern matching
 
Similar Threads
Extracting a string with particular pattern from text
filter mapping
how to check for specific string pattern(using jdk 131)
pattern matching
Remove Punctuations with regex