Re: Find string ending with %



Keenan, Greg John (Greg)** CTR ** wrote:
> Hi,

Hello,

> Need some help with a regex please.
>
> I need to search a file for every instance of a string that ends with %.
> Think output from the df command. I need the 1, 2 or 3 digits before the %
> passed into the array.
>
> Code:
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $outfile2 = "XXX.dat";
> open(DATA2, "< $outfile2") || die "Can't open $outfile2: $!\n";
> while(<DATA2>) {
> my @oput2 = /\b([0-9]+)%/;
> print "XXX @oput2 XXX\n";
> }
> close(DATA2);
>
> It is kind of working but I pick up stuff I don't want e.g:
>
> Input data(XXX.dat):
> head
> 123 123 111% 123
> 456 22% 456 456 456
> 789
> tail
>
> Output data:
> XXX XXX
> XXX 111 XXX
> XXX 22 XXX
> XXX XXX
> XXX XXX
>
> How do I tell it not to process every line in the file, just the lines that
> contain the relevant string?

while(<DATA2>) {
my @oput2 = /(\d+)%/ or next;
print "XXX @oput2 XXX\n";
}



John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: Fastest way to search a string for the occurance of a word??
    ... but the OP's question was what's the "Fastest way to search a string ... in all the tests I did here, the Regex was by far superior. ... However, of course, if you've got new regular expressions all ... Sure - but just that extra Match object could be relevant if the search ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regular expression help
    ... Basically because if you remove everything that is optional in the regex below you end up with an empty regex: ... So the regex engine will try to match on every character in the string: ... , comma doesn't match, but the nothingness in front of it does. ... A quote followed by any sequence of characters that is not a quote, ...
    (microsoft.public.dotnet.framework)
  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... match per string for either Regex. ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex Capture problem
    ... "learned" my regex using a freeware utility that had slightly different ... was trying to capture instead of. ... I have used Regex utilities before, so I understand the concepts of text ... Function RESub(str As String, SrchFor As String, ReplWith As String) As String ...
    (microsoft.public.excel.programming)
  • Re: Trim a multiple line message to a single line
    ... You can do this quite easily with either a regex or a simple function I'll try to demonstrate both: ... private string LayoutInput ... Could you send a sample file with two of these data blocks f what ...
    (microsoft.public.dotnet.languages.csharp)