in reply to Re: Steps to get a substring from a string
in thread Steps to get a substring from a string

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.
  • Comment on Re^2: Steps to get a substring from a string

Replies are listed 'Best First'.
Re^3: Steps to get a substring from a string
by AnomalousMonk (Archbishop) on Jul 22, 2015 at 01:55 UTC
    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:  <%-(-(-(-<

Re^3: Steps to get a substring from a string
by CountZero (Bishop) on Jul 22, 2015 at 06:38 UTC
    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
Re^3: Steps to get a substring from a string
by Anonymous Monk on Jul 22, 2015 at 00:03 UTC
    Let me reiterate what CountZero told you earlier
    if ($string =~ m/name=([^.]+)/) { my $variable_i_am_interested_in = $1; }