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

Hello - Perl newbie here looking for some help in string manipulation! I have a String which can either be with the "-" or without that character. String is: "This is a design-controlled string". (OR) "This is a design controlled string"; In either case, I have to extract the word "design" if Controlled is found. To determine if Controlled exists, I have the below. So, inside the IF,I'm thinking this is what I need to do.

if ($title_ele =~ m/controlled/i) {
  • #controlled exists
  • #Find the position where "controlled" starts in string
  •  $pos = index($title_ele,$controlled_key);
  • #Extract everything from <start of string> till <start of controlled>
  • #Split the remaining string by space.
  • #check if last word is "-". If it is, extract the second last word,which will be "design".
  • Is this the right approach? If so, how can I implement it using Perl? Alternately, is there an easier way that I can extract the word "design" from a string if "controlled" exists? Thanks much in advance, madbee

    Replies are listed 'Best First'.
    Re: Search and Extract from String
    by Laurent_R (Canon) on Jun 29, 2013 at 09:38 UTC

      Or simply:

       print $1 if $string =~ /(\w+)[- ]controlled/;

        Thanks, so much! This worked. Saved me tons of lines of code had I gone with my approach

    Re: Search and Extract from String
    by hdb (Monsignor) on Jun 29, 2013 at 08:25 UTC

      It is much easier to build a regex, similar to the one you use to find "controlled", that looks for the sequence of the words "design" and "controlled" with either a space or a hyphen in between. A regex to match one character or another can be constructed using the pipe character. For example, (a|b) will match a or b,

    Re: Search and Extract from String
    by poj (Abbot) on Jun 29, 2013 at 08:50 UTC

      You can a match and capture part of the string with brackets(). The captured part is stored in the variable $1. For example ;

      #!perl use strict; while (<DATA>){ # capture word with () # [- ] character is space or dash if (m/(design|otherword)[- ]controlled/i){ # captured word is in $1 print "Word < $1 >found in $_"; } else { print "No word found in $_"; } } __DATA__ This is a design controlled string This is a design-controlled string This is a controlled design string This is an otherword-controlled string
      poj
    Re: Search and Extract from String
    by 2teez (Vicar) on Jun 29, 2013 at 08:50 UTC

      Hi madbee,
      There are several ways you can use to make this happen. Though following your "outline" could make it make ( I didn't try it), but using regex could also do this:

      use warnings; use strict; while(<DATA>){ if(/controlled/i){ if(/(?<=a)\s(.+?)-?(?=controlled)/i){ #NOTE HERE print $1,$/; } } } __DATA__ This is a design-controlled string This is a design controlled string
      ..produces...
      design design
      NOTE: This might not be a generic solution, simply because am using OP data.
      Please see Using Look-ahead and Look-behind
      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me
    Re: Search and Extract from String
    by ww (Archbishop) on Jun 29, 2013 at 15:18 UTC
      Untrue assumption in the last "#" note:

      " ...
      #Split the remaining string by space.
      #check if last word is "-". If it is, extract the second last word,which will be "design".
      "

      Test case:

      perl -E "my $s = 'This is a design-controlled'; my @arr=split / /, $s; for $_(@arr) { say $_;}"

      Output (note that last word is NOT a stand-alone hyphen/dash):

      This is a design-controlled

      And the "second last word" is "a"


      If you didn't program your executable by toggling in binary, it wasn't really programming!