Re: Steps to get a substring from a string
by CountZero (Bishop) on Jul 21, 2015 at 16:48 UTC
|
Just to make sure I understand you fully.You have the following four patterns of data: - name=servername.xyz.abc.coom
- name=servername
- 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
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
|
|
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
| [reply] |
|
|
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.
| [reply] |
|
|
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: <%-(-(-(-<
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
Let me reiterate what CountZero told you earlier
if ($string =~ m/name=([^.]+)/)
{
my $variable_i_am_interested_in = $1;
}
| [reply] [d/l] |
Re: Steps to get a substring from a string
by Corion (Patriarch) on Jul 21, 2015 at 16:35 UTC
|
Depending on the parts you have already written and the functionality you need, Mozilla::PublicSuffix might help you to determine what is "a domain" and what is "a hostname in a domain".
If your question is only about string manipulation, have you considered using split on a dot and taking the first result?
my @parts = split /\./, $input;
| [reply] [d/l] |
|
|
my $str='name=host.com'
my ($match) =$string=~/=([a-zA-Z]+)/;
Update:
Updated the regex part as per CountZero's node Re^3: Steps to get a substring from a string
All is well. I learn by answering your questions...
| [reply] [d/l] |
|
|
| [reply] |
|
|
Re: Steps to get a substring from a string
by vinoth.ree (Monsignor) on Jul 21, 2015 at 16:32 UTC
|
What did you try so far? show us the code you have tried.
All is well. I learn by answering your questions...
| [reply] [d/l] |