I have only been able to come up with the match regex, but I'm not sure how to get the text at the end.

Unless you're really concerned about performance, it doesn't hurt to split things up into two regexes to make things easier to think about, for example you could say:

($number) = /(\d+)~$/ if /INC\*XX\*/;

But in this case it's not too difficult to do it in a single regex, see below.

(Update: To answer your question more basically, have a look at perlretut, where capturing groups are explained.)

Inside my perl script I assume that I will use something like this

It's almost never necessary to call perl from inside a Perl script. The while loop in the following code is the equivalent of a one-liner. I've added some checks to make sure that the number is found exactly once in the input file - I use defined instead of a boolean check to differentiate between 0, which Perl would consider false, and undef, which in this case means that no number was found. In the regex, I'm using a couple of techniques: /x for readability, and a negative lookbehind (?<!\d) to make sure the thing before the number is not also a number - not really required in this case (it's a bit of paranoia), but it could become an important technique if your match happens to get more complex. You didn't say whether there could be any characters in the string between "INC*XX*" and the number; if not, then the regex could be simplified to just /INC\*XX\*(\d+)~$/.

use warnings; use strict; use Data::Dumper; $Data::Dumper::Useqq=1; my $filename = 'cr835.txt'; open my $fh, '<', $filename or die "$filename: $!"; my $number; while (<$fh>) { if ( m{ INC\*XX\* .* (?<!\d) (\d+) ~ $ }x ) { die "number matched twice" if defined $number; $number = $1; } } die "no number found" unless defined $number; close $fh; print Dumper($number);

In reply to Re: Print text on the same line after match by haukex
in thread Print text on the same line after match by periodicalcoder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.