On Thu, Jul 28, 2011 at 7:57 AM, kotin 76 [email protected] wrote:
#######################################################################
I would appreciate any answer.
Based on this question and some previous ones, I think you need to
read a little bit about regular expressions. For example:
http://www.regular-expressions.info is a good starting point.
For you to understand why the above code is failing, you need to know
that \w+ matches any “word character” (the \w), one or more times (the
+). And why is it stopping at the “-”? Because “-” is not a word
character, so the regular expression stops matching there.
To come up with a correct regular expression, you need to understand
all the possible scenarios for that string. For example, maybe you can
match all word characters, the dash, the underscore and the “.”. Or
maybe just the word characters plus the dash is enough. So please, let
us know in plain English what your requirements are. For example:
In a string such as “/dev/sdb 1949612 102404 1847208 6%
/media/4B22-5451”, I need to match everything after “/media/”, which
could be letters, numbers and dashes. Or:
In a string such as “/dev/sdb 1949612 102404 1847208 6%
/media/4B22-5451”, I need to match everything after “/media/” up to
the end of the line. Or:
In a string such as “/dev/sdb 1949612 102404 1847208 6%
/media/4B22-5451”, I need to match everything after “/media/”, up to
the next whitespace or end of the line.
I hope you get what I mean. You need to precisely define what are the
conditions on which the matching has to stop or continue. Several
examples based on the above:
For the first one:
%r{/media/[-\w]+} # match the literal /media/ followed by one or
more of word characters or dashes
%r{/media/[-a-zA-Z0-9]+} # match the literal /media/ followed by one
or more of dash, lower or uppercase letters or numbers
For the second:
%r{/media/.*} # match the literal /media/ followed by anything else up
to the end of the line
For the third:
%r{/media/[\S]*} # match the literal /media/ followed by anything else
that is not a whitespace
Also, even if you use the correct regexp for your case, you need to
brush up the logic a little bit:
string1 = “/dev/sdb 1949612 102404 1847208 6% /media/4B22-5451”
if m2=string1.match( %r{/media/\w+} ).to_s then
if m2 =~ %r{/media/} then
puts “pass”
puts “mediastring = #{m2}”
end
end
To understand if the string matched or not while keeping the match
data object, the idiom I use is:
if m = string1.match(regexp)
the matched string is now in m[0]
end
Also, you don’t need the second if, because the regexp is a subset of
the first one, so any string matched by the first regexp will match
the second one. Again, I’d do:
if m = string1.match(regexp)
the matched string is now in m[0]
puts “pass”
puts “media string = #{m[0]}”
end
Now, you just need to come up with the correct regexp for all your
possible cases and you are set.
Hope this helps,
Jesus.