Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow Monks!
I am running a pattern match string, in order to look for substrings like:
weight gain

in strings like:
4;T;X;15;O20;O26.0;O26.0;O260;Excessive weight gain in pregnancy;242;U +NDEF;UNDEF;2-067;1-089 4;T;X;15;O20;O26.1;O26.1;O261;Low weight gain in pregnancy;242;UNDEF;U +NDEF;2-067;1-089

Performing the pattern match is easy of course, just by:
if($big_string=~/$small_string/) {print "Found match\n";}

however, I seem to not be able to do exactly what I need, and that is that I would like to keep only:
Excessive weight gain in pregnancy Low weight gain in pregnancy
from the afore-mentioned "big strings". any hints on how to do that?

Replies are listed 'Best First'.
Re: How can I remove unwated parts of a string in a pattern match?
by choroba (Cardinal) on Dec 14, 2015 at 11:58 UTC
    If no quoting and escaping is involved, you can just split on semicolon:
    #! /usr/bin/perl use warnings; use strict; my @strings = ('4;T;X;15;O20;O26.0;O26.0;O260;Excessive weight gain in + pregnancy;242;UNDEF;UNDEF;2-067;1-089', '4;T;X;15;O20;O26.1;O26.1;O261;Low weight gain in pregn +ancy;242;UNDEF;UNDEF;2-067;1-089'); for my $string (@strings) { my $text = (split /;/, $string)[8]; print $text, "\n" if $text =~ /weight gain/; }

    Otherwise, see Text::CSV.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: How can I remove unwated parts of a string in a pattern match?
by Anonymous Monk on Dec 14, 2015 at 12:37 UTC
    Got it!
    I ran:
    if($ig_string=~/.*\;(.*?$small_string.*?)\;/)
      Hi,

      although your regex most probably works fine, the initial part (.*) is useless if you can describe better what you want to match, and the semi-colons don't need to be escaped. You could consider something like this:

      if ($big_string =~ /;([\w ]+$small_string[\w ]+);/)