Hi,
i have a string,
str = “ftp://XYZ/a/b/c/d”
i need to extract “ftp://XYZ” . Can anyone suggest a simple reg
expression?
Hi,
i have a string,
str = “ftp://XYZ/a/b/c/d”
i need to extract “ftp://XYZ” . Can anyone suggest a simple reg
expression?
This is one approach if you have a reason for preferring a regex:
str.match /afp://[A-Z]{3}/
However, keep in mind that without more examples it’s hard to say
whether this actually meets your requirements. For instance, AFP
protocol does not require hostnames to be all caps. Nor is it a given
that your intention is to extract from a dataset in which domains
always have exactly 3 letters.
Unless you have a reason for using a regex and have specced it well,
Eric’s solution is likely to be the more robust one.
On Jun 12, 2012, at 13:24, cyber c. wrote:
i have a string,
str = “afp://XYZ/a/b/c/d”
i need to extract “afp://XYZ” . Can anyone suggest a simple reg
expression?
URI can do this.
$ ruby -ruri -e ‘u = URI(“afp://XYZ/a/b/c/d”); p u.scheme, u.host’
“afp”
“XYZ”
and:
$ ruby -ruri -e ‘u = URI(“afp://XYZ/a/b/c/d”); puts u + “/”’
afp://XYZ/
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs