in reply to Steps to get a substring from a string

Just to make sure I understand you fully.

You have the following four patterns of data:

  1. name=servername.xyz.abc.coom
  2. name=servername
  3. name=servername.xyz.com
and for each of these patterns you want to extract servername.

If the above assumptions are correct, the following will do the trick:

use Modern::Perl qw/2015/; while (<DATA>) { chomp; say "$_ => $1" if m/name=([^\.]+)/; } __DATA__ name=servername.xyz.abc.coom name=servername name=servername.xyz.com
Output:
name=servername.xyz.abc.coom => servername name=servername => servername name=servername.xyz.com => servername
update:

To be fully compliant with the respective RFCs (see above), the regex should be m/name=([-0-9a-z]+)/i

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Steps to get a substring from a string
by Not_a_Number (Prior) on Jul 21, 2015 at 17:34 UTC
    if m/name=([^\.]+)/;

    Small remark: there's no need to escape '.' (dot) in a character class.

      I know. It is just a (bad?) habit I have and to be a bit more orthogonal between the use is "." inside and outside of character classes.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
Re^2: Steps to get a substring from a string
by shroh (Acolyte) on Jul 21, 2015 at 23:21 UTC
    Hi Let me reiterate what i told earlier, We have three scenarios: 1) Variable a can be servername.abc.xyz.com 2) It can be servername.xyz.com 3) It can be just servername I all the above cases i am just intereested in servername and if it can be saved in a variable.
      Let me reiterate ...

      Unfortunately, you're not reiterating anything: none of the example strings you give is the same as any in the OP. I suggest you follow CountZero's approach and post something like

      I have the following patterns of data:
      • 'name=servername.xyz.abc.coom'
      • 'name=servername'
      • 'name=servername.xyz.com'
      • 'whatever'
      • ...
      From each of these patterns I want to extract string.
      I think I know what you think you want, but why should we play guessing games? The devil is in the details.

      Update: Also, what version(s) of Perl must you work with? This has implications for the regex constructs that are available.


      Give a man a fish:  <%-(-(-(-<

      Wait! You have just changed the data. Before it was of the general form "name=servername ..." and now it is "servername ...".

      Different forms of data will need different forms of regexes.

      Please provide some better information or our help will just be guesswork.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
      Let me reiterate what CountZero told you earlier
      if ($string =~ m/name=([^.]+)/) { my $variable_i_am_interested_in = $1; }