From: Chad Perrin Date: 2011-07-28T02:54:38+09:00 Subject: Re: if condition for pattern matching --EuxKj2iCbKjpUGkD Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Jul 28, 2011 at 01:55:37AM +0900, kotin 76 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 =3D File.readlines("dev_string2.txt") > > 8 > > 9 > > 10 for i in dev2 > > 11 devstring=3Di.match(/\/dev\/[\w]+/) > > 12 puts devstring > > 14 if devstring =3D~ %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 =3D File.dreadlines("dev_string2.txt") for i in dev2 devstring =3D i.match(/\/dev\/[\w]+/) puts devstring if devstring =3D~ %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 =3D 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 =3D File.dreadlines("dev_string2.txt") for i in dev2 devstring =3D i.match( %r{/dev/\w+} ).to_s puts devstring =20 if devstring =3D~ %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 =3D File.dreadlines("dev_string2.txt") for i in dev2 devstring =3D i.match %r{/dev/\w+} puts devstring =20 if devstring.to_s =3D~ %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. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --EuxKj2iCbKjpUGkD Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk4wTOAACgkQ9mn/Pj01uKX03wCdEiM7LXlobOGKpxe9aknc6iQg uSUAoM68nixBnaRHsrmFhZQj/qBIX4F6 =WIyy -----END PGP SIGNATURE----- --EuxKj2iCbKjpUGkD--