If condition for pattern matching

Hi All,

i have below program

···

###############################
string1 = /dev/sda1

if string1 =~ /\/dev\/ then
   puts pass
end
###############################

my aim is if dev find in that string it should enter into conditon and
should print pass.

but i getting error.

can anyone suggest the logic for this.

i would appreciate any replys

Regards
kotin

--
Posted via http://www.ruby-forum.com/.

Hi All,

i have below program

###############################
string1 = /dev/sda1

You need to quote the string, i.e.
string1 = '/dev/sda1'

Dave.

···

On 27 Jul 2011, at 16:37, kotin 76 wrote:

if string1 =~ /\/dev\/ then
  puts pass
end
###############################

my aim is if dev find in that string it should enter into conditon and
should print pass.

but i getting error.

can anyone suggest the logic for this.

i would appreciate any replys

Regards
kotin

--
Posted via http://www.ruby-forum.com/\.

you need "" or '' around your string

string1 = "/dev/sda1" or string1 = '/dev/sda1'

···

--
Posted via http://www.ruby-forum.com/.

Dave Baldwin wrote in post #1013388:

You need to quote the string, i.e.
string1 = '/dev/sda1'

Dave.

  if "pass" is not a method or a local variable, you will also need to
quote that...

string1 = "/dev/sda1"

if string1 =~ /\/dev\// then
   puts "pass"
end

  Rob's suggestion from your string manipulation post is also a handy
one, i find reading regexes easier when they are written as %r{/dev/},
rather than /\/dev\//

  - j

···

--
Posted via http://www.ruby-forum.com/\.

Hi all,

i tried as per you suggestions from below program

  1 string1 = '/dev/sda1'
  2
  3 if string1 =~ //dev\ then
  4 puts pass
  5 end

But i getting below error. i think i may done mistake at regular
expresson under if condition. Please sugget regular expression under if
condtion to enter in to loop. i am looking for regular expression to
enter into loop if it find "dev" in given string.

···

########################################
7.rb:3: unknown regexp options - dv
7.rb:3: syntax error, unexpected $undefined, expecting keyword_then or
';' or '\n'
if string1 =~ //dev\ then
                    ^
7.rb:5: syntax error, unexpected keyword_end, expecting $end
##########################################

--
Posted via http://www.ruby-forum.com/.

hi ,

as per jake reply,

i tried the below code

  1 string1 = '/dev/sda1'
  2
  3 if string1 =~ %r{/dev/} then
  4 puts "pass"
  5 end

its working fine.

Thanks every one for quick replys.

Regards
kotin

kotin 76 wrote in post #1013391:

···

Hi all,

i tried as per you suggestions from below program

  1 string1 = '/dev/sda1'
  2
  3 if string1 =~ //dev\ then
  4 puts pass
  5 end

But i getting below error. i think i may done mistake at regular
expresson under if condition. Please sugget regular expression under if
condtion to enter in to loop. i am looking for regular expression to
enter into loop if it find "dev" in given string.

########################################
7.rb:3: unknown regexp options - dv
7.rb:3: syntax error, unexpected $undefined, expecting keyword_then or
';' or '\n'
if string1 =~ //dev\ then
                    ^
7.rb:5: syntax error, unexpected keyword_end, expecting $end
##########################################

--
Posted via http://www.ruby-forum.com/\.

Hi ,

i applying same above logic for below program

  1
  2 #!/usr/bin/env ruby
  3
  5 dev2 = File.readlines("dev_string2.txt")
  8
  9
10 for i in dev2
11 devstring=i.match(/\/dev\/[\w]+/)
12 puts devstring
14 if devstring =~ %r{/dev/} then
15 puts "pass"
16 end
17 end

but i am not able to print pass. pleas suggest any thing to print pass

pleas find the attached file of dev_string2.txt

Regards
kotin

Attachments:
http://www.ruby-forum.com/attachment/6455/dev_string2.txt

···

--
Posted via http://www.ruby-forum.com/.

Hi any one can reply for this?

Regards
kotin

kotin 76 wrote in post #1013393:

···

Hi ,

i applying same above logic for below program

  1
  2 #!/usr/bin/env ruby
  3
  5 dev2 = File.readlines("dev_string2.txt")
  8
  9
10 for i in dev2
11 devstring=i.match(/\/dev\/[\w]+/)
12 puts devstring
14 if devstring =~ %r{/dev/} then
15 puts "pass"
16 end
17 end

but i am not able to print pass. pleas suggest any thing to print pass

pleas find the attached file of dev_string2.txt

Regards
kotin

--
Posted via http://www.ruby-forum.com/\.

kotin 76 wrote in post #1013393:

Hi ,

i applying same above logic for below program

  1
  2 #!/usr/bin/env ruby
  3
  5 dev2 = File.readlines("dev_string2.txt")
  8
  9
10 for i in dev2
11 devstring=i.match(/\/dev\/[\w]+/)
12 puts devstring
14 if devstring =~ %r{/dev/} then
15 puts "pass"
16 end
17 end

hi Kotin -

  have a look here: http://www.ruby-doc.org/core/classes/File.html, and
here: class IO - RDoc Documentation, for more
info on ruby's File methods...

  i would use File.open with a block for this operation - something like
this:

File.open("dev_string2.txt", "r"){|file|
  file.each{|line|
    if line =~ %r{/dev/}
      puts "pass: #{line}"
    end
  }
}

  note that running this the second line of your file does not pass, as
it ends in "/dev", and not "/dev/". i'm not sure if this is your
intended result. if you want the second line to pass as well, you could
just change your regex to %r{/dev}.

  - j

···

--
Posted via http://www.ruby-forum.com/\.

Hi any one can reply for this?

1
2 #!/usr/bin/env ruby
3
5 dev2 = File.readlines("dev_string2.txt")
8
9
10 for i in dev2
11 devstring=i.match(/\/dev\/[\w]+/)

String#match returns a MatchData object. If you want the matched
string use [0] on that. Also, I recommend using another name for i,
something like line. And also use File.foreach if you are doing things
to lines one by one, so that you don't read the entire file into
memory.

12 puts devstring
14 if devstring =~ %r{/dev/} then

The result of the match of line 11 is obviously going to match this
regular expression too, since it is a subset of the other one.

15 puts "pass"
16 end
17 end

but i am not able to print pass. pleas suggest any thing to print pass

So, what I'd do:

File.foreach("dev_string2.txt") do |line|
  if m = line.match(%r{/dev/[\w]+})
    puts "line #{line} passed the test"
  end
end

Hope this helps,

Jesus.

···

On Wed, Jul 27, 2011 at 6:55 PM, kotin 76 <kotin76@yahoo.com> wrote:

Hi any one can reply for this?

Give people a chance to read, formulate a response, and reply. Sometimes
it takes time. We are all members of a community here who have lives
outside of this mailing list; we are not some organization's customer
support channel.

kotin 76 wrote in post #1013393:
> Hi ,
>
> i applying same above logic for below program
>
> 1
> 2 #!/usr/bin/env ruby
> 3
> 5 dev2 = File.readlines("dev_string2.txt")
> 8
> 9
> 10 for i in dev2
> 11 devstring=i.match(/\/dev\/[\w]+/)
> 12 puts devstring
> 14 if devstring =~ %r{/dev/} then
> 15 puts "pass"
> 16 end
> 17 end

You may want to try using more consistent formatting for your code in
line with common Ruby idioms. Taking care with code formatting makes
your code easier to read, which makes people more likely to reply.

If I eliminate the line numbers, your code might better be formatted
thusly (with the whole code sample indented by four spaces in this case
to set it off from the main text of the email):

    dev2 = File.dreadlines("dev_string2.txt")

    for i in dev2
      devstring = i.match(/\/dev\/[\w]+/)
      puts devstring

      if devstring =~ %r{/dev/} then
        puts "pass"
      end
    end

With this clearer formatting, it is easier at a glance for most Rubyists
to see where your code might be cleaned up and altered to make it do what
you want it to do.

In addition, you might want to use the %r syntax for the regex used in
the i.match expression, as well as in the devstring matching expression,
and you might want to leave out the unnecessary brackets in that regex:

    devstring = i.match %r{/dev/\w+}

>
> but i am not able to print pass. pleas suggest any thing to print pass

Your devstring variable does not actually contain a string. It contains
a MatchData object, because that is what your i.match expression returns.
A MatchData object for %r{/dev/\w+} will not match a string, so your
later devstring matching expression fails to find a match. You must
change your MatchData object into a string in one of at least two places
to make the minimally effective change to your code.

One place is where the first regular expression is used, resulting in
code like the following:

    dev2 = File.dreadlines("dev_string2.txt")

    for i in dev2
      devstring = i.match( %r{/dev/\w+} ).to_s
      puts devstring
      
      if devstring =~ %r{/dev/} then
        puts "pass"
      end
    end

Another is to change it where your second regular expression is used,
resulting in code like the following:

    dev2 = File.dreadlines("dev_string2.txt")

    for i in dev2
      devstring = i.match %r{/dev/\w+}
      puts devstring
      
      if devstring.to_s =~ %r{/dev/} then
        puts "pass"
      end
    end

>
> pleas find the attached file of dev_string2.txt

I have not looked at this file, so I am not 100% certain my code samples
will solve all your problems here, but hopefully my code samples will
help you understand the problem you have been having.

There are other changes I might make to this code, but I do not want to
clutter this email's code samples with too much information that
distracts from the central problems at hand.

···

On Thu, Jul 28, 2011 at 01:55:37AM +0900, kotin 76 wrote:

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

Hi All,

Thanks for all your quick support.

now i got solutions.

Thanks very much

Regards
kotin

···

--
Posted via http://www.ruby-forum.com/.