Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a regex that looks for certain types of hostnames like: .*-.*(nmtg)*|.*(\.nms). How do I modify this so it does not match: 11.22:33:44:55-66?
Should match:
cs25-admin.nmtg.company.com
cs25-admin
but should not match:
11.22:33:44:55-66
Two basic ways:
You can replace your "match anything" . with "match anything except for colon" [^:] everywhere
You can prepend your expression with "no colons from here to the end of string" (?!.*:)
EDIT As Signus said, your regexp is really non-specific and open-ended; it will match much more than what you think. For example, "----THRICEnmtgnmtgnmtg" is a full match, and so is "(-_-)". It is a better policy and easier to carefully specify what you want, rather than go listing exceptions. The regexps suggested by Signus are a good example.
They will still match within strings: "dont match this: example.com" will still match the "example.com" part. If that is what you want, cool. If not, you want to anchor the start and end of the string, by surrounding your regexp with /^.....$/.
You're using the * quantifier that matches 0 or more of the preceding token, in which case you supplied . which is a token that matches any character except line breaks.
To match domain names with subdomain names you can do the following:
(\w+\.)?\w+\.(com|org)
And to really match any domain with a TLD I like to do this:
([a-zA-Z0-9]+\.){1,2}[a-zA-Z]{2,4}
Where the latter will match any domain with a single subdomain using the numeric quantifer {num} which allows you to specify a range of matches, as shown in the above regex.
This allows you to match a group of alphanumeric characters followed by a period 1 to 2 times (i.e. subdomain.domain.topleveldomain, where subdomain. is the first match and domain. is the second match of the first group).
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
please is in ruby possible to get information from example name "Doe,Jon" (exact format) to get only the name "Jon"? Of course the name can be always different, I was thinking if is not possible to get the value from end of string to "," separator. If is it possible, how?
Thanks for your help.
So lets examine some of the solutions that are given to you in the comments
Split
"Doe,Jon".split(',').last
# or a bit more verbose
parts = "Doe,Jon".split(',') # ["Doe", "Jon"]
name = parts.last # "Jon"
String#split splits a sting into an array. It uses the parameter "," as separator. Array#last returns the last item from an array.
Gsub
"Doe,Jon".gsub(/.*,/, '')
String#gsub substitutes the part that matches the Regular Expression (/.*,/) with the substitution value ("").
The regexp matches everything (.*) up to (and including) the comma. And the replacement is an empty string, essentially deleting the part that matches the regexp.
Note that you could/should probably have an anchor to make the regexp more strict (/\A.*,/)
Slice
String#slice creates a substring given a range. -1 is a shortcut for the last element.
String#index finds the index of a character inside a String.
"Doe,Jon".slice(("Doe,Jon".index(',')+1)..-1)
# or more verbose
full = "Doe,Jon"
index_of_comma = full.index(',') # => 3
index_after_comma = index + 1
name = full.slice(index_after_comma..full.size)
CSV
CSV (Comma Separated Values) is a format where multiple values are separated by a comma (or other separation character).
require "csv"
CSV.parse("John,Doe")[0][1]
This will treat the name as CSV data and then access the first row of data (´[0]´). And from that row accesses the second element ([1]) which is the name.
Now what?
There are usually multiple ways to reach a goal. And it's up to you to pick a way. I'd go with the first one. To me it is easy to read and understand its purpose.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I write a searching command using grep that will look for a line with a strict requirements. For example it should start with a name, which consist only letters and "-", then follows an ":", then a year or "xxxx", then again an ":", and then a line of letters, digits and "-" of some length. Or may be there is a link where I can read this... I'm trying to find some solution in the Internet for a long time, but can't...
What you need here is to pass the grep command a regular expression that describes your pattern of interest, on the basis of which grep will match only valid lines.
Taking into account your indications, the following regular expression could do the job:
^([A-z]|-)+:([0-9]|xxxx)+:([A-z]|[0-9]|-)+$
The expression begins and ends with the ^ and $ anchors, that indicate the beginning and the end of a line. Then, you basically have three token blocks, separated by :, the first matching letters and dashes, the second years or xxxx, and the third letters, digits and dashes. + is a quantifier, indicating that the preceding token can appear one or more times.
You can use it with grep like so:
grep -P "^([A-z]|-)+:([0-9]|xxxx)+:([A-z]|[0-9]|-)+$"
The -P option is to indicate to interpret it as a Perl regex and correctly handle hyphens matching.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i am constructing a program in Ruby which requires the value to be extracted between the 2nd and 3rd full-stop in a string.
I have searched online for various related solutions, including truncation and this prior Stack-Overflow question: Get value between 2nd and 3rd comma, however no answer illustrated a solution in the Ruby language.
Thanks in Advance.
list = my_string.split(".")
list[2]
That will do it I think. First command splits it into a list. Second gets the bit you want
You could split the string on full stops (aka periods), but that creates an array with one element for each substring preceding a full stop. If the document had, say, one million such substrings, that would be a rather inefficient way of getting just the third one.
Suppose the string is:
mystring =<<_
Now is the time
for all Rubiests
to come to the
aid of their
bowling team.
Or their frisbee
team. Or their
air guitar team.
Or maybe something
else...
_
Here are a couple of approaches you could take.
#1 Use a regular expression
r = /
(?: # start a non-capture group
.*?\. # match any character any number of times, lazily, followed by a full stop
){2} # end non-capture group and perform operation twice
\K # forget everything matched before
[^.]* # match everything up to the next full stop
/xm # extended/free-spacing regex definition mode and multiline mode
mystring[r]
#=> " Or their\nair guitar team"
You could of course write the regex:
r = /(?:.*?\.){2}\K[^.]*/m
but the extended form makes it self-documenting.
The regex engine will step through the string until it finds a match or concludes that there can be no match, and stop there.
#2 Pretend a full stop is a newline
First suppose we were looking for the third line, rather than the third substring followed by a full stop. We could write:
mystring.each_line.take(3).last.chomp
# => "to come to the"
Enumerable#take determines when a line ends by examining the input record separator, which is held by the global variable $/. By default, $/ equals a newline. We therefore could do this:
irs = $/ # save old value, normally \n
$/ = '.'
mystring.each_line.take(3).last[0..-2]
#=> " Or their\nair guitar team"
Then leave no footprints:
$/ = irs
Here String#each_line returns an enumerator (in effect, a rule for determining a sequence of values), not an array.
This question already has answers here:
How to extract URLs from text
(6 answers)
Closed 8 years ago.
I am tring to extract a link from a phrase and it could be any where last, first or middle so I am usig this regex
link=text.scan(/(^| )(http.*)($| )/)
but the problem is when the link is in the middle it gets the whole phrase until the end.
What should I do ?
It's because .* next to http is greedy. I suggest you to use lookarounds.
link=text.scan(/(?<!\S)(http\S+)(?!\S)/)
OR
link=text.scan(/(?<!\S)(http\S+)/)
Example:
> "http://bar.com foo http://bar.com bar http://bar.com".scan(/(?<!\S)http\S+(?!\S)/)
=> ["http://bar.com", "http://bar.com", "http://bar.com"]
DEMO
(?<!\S) Negative lookbehind which asserts that the match won't be preceeded by a non-space character.
http\S+ Matches the substring http plus the following one or more non-space characters.
Do all the links you are trying to match follow some simple pattern? We'd need to see more context to confidently provide a good solution to your problem.
For example, the regex:
link=text.scan(/http.*\.com/)
...might be good enough for the job (this assumes all links end in ".com"), but I can't say for sure without more information.
Or again, for example, perhaps you could use something like:
link=text.scan(/http[a-z./:]*) - this assumes all links contain only lower case letters, ".", "/" and ":".
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to validate version numbers, e.g., 6.0.2/6.0.2.011 when a user enters them.
I checked to_i, but it doesn't serve my purpose. What can be a way to validate the version number? Can anyone let me know?
Here's a regular expression that matches a "valid" version number as per your specifications (only numbers separated by .):
/\A\d+(?:\.\d+)*\z/
This expression can be broken down as follows:
\A anchor the expression to the start of the string
\d+ match one or more digit character ([0-9])
(?: begin a non-capturing group
\. match a literal dot (.) character
\d+ match one or more digit character
)* end the group, and allow it to repeat 0 or more times
\z anchor the expression to the end of the string
This expression will only allow . when followed by at least one more number, but will allow any number of "sections" of the version number (ie. 6, 6.0, 6.0.2, and 6.0.2.011 will all match).
If you want to work with version numbers, I advise the versionomy (Github) gem.
See if this helps.
if a.length == a.scan(/\d|\./).length
# only dots and numbers are present
# do something
else
# do something else
end
e.g
a = '6.0.2.011'
a.length == a.scan(/\d|\./).length #=> true
b = '6b.0.2+2.011'
b.length == b.scan(/\d|\./).length #=> false
input length is checked against the scan outcome's length to ensure only dot and numbers are present. Having said that, it is very hard to guarantee that future version numbers will all follow the same conventions. How will you make sure that some one does not introduce something like 6a.0.2.011