Siimple string manuplation logic. But i did not getting correct one

Hi All,

I am new for ruby.

from below program i am trying to print /media/4B22-5451 ( i mean from
/media to end of that word). But i am able to print only up to
/media/4B22.

please any once can help me to print upto /media/4B22-5451

#######################################################################
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
#######################################################################

I would appreciate any answer.

Regards
Kotin

On Thu, Jul 28, 2011 at 1:57 PM, kotin 76 [email protected] wrote:

string1 = “/dev/sdb 1949612 102404 1847208 6% /media/4B22-5451”
if m2=string1.match( %r{/media/\w+} ).to_s then

careful here. the condition returns a string, ergo, it will always
return true.
also \w does not include the dash char

if m2 =~ %r{/media/} then

you may not need this additional if

puts “pass”
puts “mediastring = #{m2}”
end
end

try eg,

string1 = “/dev/sdb 1949612 102404 1847208 6% /media/4B22-5451”
if m2=string1.match( %r{/media/.+} )
puts “mediastring=#{m2}”
end

output:
mediastring=/media/4B22-5451

there are simpler ways to solve your objective, but we think you will
get there.

happy rubying and best regards,
-botp

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.

Hi all,

thanks for all and especially for Gabriel.

I got solutions as per your suggestions.

Gabriel :

for my requirement i need second one

For the second:

%r{/media/.*} # match the literal /media/ followed by anything else up
to the end of the line

Its really great full for your support

Regards
Kotin

“Jesús Gabriel y Galán” [email protected] wrote in post
#1013491:

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.