# The Regular exp read from file not matching!

**URL:** <https://www.ruby-forum.com/t/the-regular-exp-read-from-file-not-matching/142991>\
**Category:** Ruby\
**Created:** [July 14, 2008, 9:40am UTC](https://www.ruby-forum.com/t/the-regular-exp-read-from-file-not-matching/142991 "2008-07-14T09:40:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shambolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@shambolic](https://www.ruby-forum.com/u/shambolic)\
**Post date:** [July 14, 2008, 9:40am UTC](https://www.ruby-forum.com/t/the-regular-exp-read-from-file-not-matching/142991/1 "2008-07-14T09:40:59Z")

</div>

I am trying to read the syntax(here the regular expression) frpm a file.

When I use /^[0-1]{1}$/ with “1” as subject it works but  
/^[0-1]{1}[a-zA-Z]{1,25}$/ syntax for “1,abcd” , I have checked out on  
the interpreter directly both works fine. I suppose I am messing up  
something with the files, or gets or chomp or …

Any help ?

```
 def sanity_check(param)
      filename = create_filename("Syntax",param) #this fn gets a

```

valid file name  
if File.exist?(filename)  
File.open(filename,“r+”) do |syntaxfile|  
syntax = syntaxfile.gets.chomp  
return syntax.match(@newsettings.value)  
end  
end  
return true # RETURN TRUE IF SYNTAX FILE NOT FOUND  
end

==============================================================================

setprm\_0.1.2.rb:149: return value  
(rdb:1) v l  
filename =\> “C:\rubypgm\Paramsyntax\prm.102”  
param =\> “102”  
syntax =\> “/^[0-1]{1}[a-zA-Z]{1,25}$/”  
value =\> nil  
(rdb:1) n  
setprm\_0.1.2.rb:85: update\_status(("\n Incorrect syntax ,  
File not up

---

<div class="post-metadata">

**Author:** ![shambolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@shambolic](https://www.ruby-forum.com/u/shambolic)\
**Post date:** [July 14, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/the-regular-exp-read-from-file-not-matching/142991/2 "2008-07-14T14:19:28Z")

</div>

On 14.07.2008, at 09:38, Jayan Jacob wrote:

> When I use /^[0-1]{1}$/ with “1” as subject it works but  
> /^[0-1]{1}[a-zA-Z]{1,25}$/ syntax for “1,abcd” , I have checked out on  
> the interpreter directly both works fine. I suppose I am messing up  
> something with the files, or gets or chomp or …
> 
> Any help ?

That RegEx: /^[0-1]{1}[a-zA-Z]{1,25}$/ doesn’t match ‘,’.

maybe: add ‘,’  
irb\> regex2=/^[0-1]{1},[a-zA-Z]{1,25}$/  
=\> /^[0-1]{1},[a-zA-Z]{1,25}$/  
irb\> regex2.match “1,abcd”  
=\> #\<MatchData “1,abcd”\>

or ‘.?’  
irb\> regex3=/^[0-1]{1}.?[a-zA-Z]{1,25}$/  
=\> /^[0-1]{1}.?[a-zA-Z]{1,25}$/  
irb\> regex3.match “1,abcd”  
=\> #\<MatchData “1,abcd”\>

hth. regards, Sandor  
Szücs

---

<div class="post-metadata">

**Author:** ![shambolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@shambolic](https://www.ruby-forum.com/u/shambolic)\
**Post date:** [July 15, 2008, 4:17am UTC](https://www.ruby-forum.com/t/the-regular-exp-read-from-file-not-matching/142991/3 "2008-07-15T04:17:04Z")

</div>

Sorry for the typo error in the earlier mail , I did try “1abcd” on  
/^[0-1]{1}[a-zA-Z]{1,25}$/. I am putting a regular expression in a file  
and my program picks up this string to do a match with the input an  
string ==\> “syntax.match(@newsettings.value)”

here is the debug info for more clarity

(rdb:1) n  
setprm\_0.1.2.rb:149: return value  
(rdb:1) v l  
filename =\> “C:\rubypgm\Paramsyntax\prm.102”  
newsettings =\> “1abcd”  
param =\> “102”  
syntax =\> “/^[0-1]{1}[a-zA-Z]{1,25}$/”  
syntaxfile =\> #\<File:C:\rubypgm\Paramsyntax\prm.102\>  
value =\> nil  
(rdb:1) l  
[144, 153] in setprm\_0.1.2.rb  
144 if File.exist?(filename)  
145 File.open(filename,“r+”) do |syntaxfile|  
146 syntax = syntaxfile.gets.chomp  
147 newsettings = @newsettings.value  
148 value = syntax.match(newsettings)  
=\> 149 return value  
150 end  
151 end  
152 return true # RETURN TRUE IF SYNTAX FILE NOT  
FOUND  
153 end

thanks  
jn

Jay Jkb wrote:

> I am trying to read the syntax(here the regular expression) frpm a file.
> 
> When I use /^[0-1]{1}$/ with “1” as subject it works but  
> /^[0-1]{1}[a-zA-Z]{1,25}$/ syntax for “1,abcd” , I have checked out on  
> the interpreter directly both works fine. I suppose I am messing up  
> something with the files, or gets or chomp or …
> 
> Any help ?
> 
> ```
> def sanity_check(param)
> filename = create_filename("Syntax",param) #this fn gets a
> 
> ```
> 
> valid file name  
> if File.exist?(filename)  
> File.open(filename,“r+”) do |syntaxfile|  
> syntax = syntaxfile.gets.chomp  
> return syntax.match(@newsettings.value)  
> end  
> end  
> return true # RETURN TRUE IF SYNTAX FILE NOT FOUND  
> end
> 
> ==============================================================================
> 
> setprm\_0.1.2.rb:149: return value  
> (rdb:1) v l  
> filename =\> “C:\rubypgm\Paramsyntax\prm.102”  
> param =\> “102”  
> syntax =\> “/^[0-1]{1}[a-zA-Z]{1,25}$/”  
> value =\> nil  
> (rdb:1) n  
> setprm\_0.1.2.rb:85: update\_status(("\n Incorrect syntax ,  
> File not up

---

<div class="post-metadata">

**Author:** ![shambolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@shambolic](https://www.ruby-forum.com/u/shambolic)\
**Post date:** [July 15, 2008, 5:12am UTC](https://www.ruby-forum.com/t/the-regular-exp-read-from-file-not-matching/142991/4 "2008-07-15T05:12:00Z")

</div>

I replaced the code with  
return /#{syntaxfile.gets.chomp}/.match(@newsettings.value)  
and it worked. for some reason when i assign it to the variable syntax  
and then use it id does not work …

some explanation on why? , would be great .

thanx  
Jn

Jay Jkb wrote:

> I am trying to read the syntax(here the regular expression) frpm a file.
> 
> When I use /^[0-1]{1}$/ with “1” as subject it works but  
> /^[0-1]{1}[a-zA-Z]{1,25}$/ syntax for “1,abcd” , I have checked out on  
> the interpreter directly both works fine. I suppose I am messing up  
> something with the files, or gets or chomp or …
> 
> Any help ?
> 
> ```
> def sanity_check(param)
> filename = create_filename("Syntax",param) #this fn gets a
> 
> ```
> 
> valid file name  
> if File.exist?(filename)  
> File.open(filename,“r+”) do |syntaxfile|  
> syntax = syntaxfile.gets.chomp  
> return syntax.match(@newsettings.value)  
> end  
> end  
> return true # RETURN TRUE IF SYNTAX FILE NOT FOUND  
> end
> 
> ==============================================================================
> 
> setprm\_0.1.2.rb:149: return value  
> (rdb:1) v l  
> filename =\> “C:\rubypgm\Paramsyntax\prm.102”  
> param =\> “102”  
> syntax =\> “/^[0-1]{1}[a-zA-Z]{1,25}$/”  
> value =\> nil  
> (rdb:1) n  
> setprm\_0.1.2.rb:85: update\_status(("\n Incorrect syntax ,  
> File not up

---

<div class="post-metadata">

**Author:** ![shambolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@shambolic](https://www.ruby-forum.com/u/shambolic)\
**Post date:** [July 15, 2008, 11:55am UTC](https://www.ruby-forum.com/t/the-regular-exp-read-from-file-not-matching/142991/5 "2008-07-15T11:55:53Z")

</div>

On 15.07.2008, at 05:07, Jay Jkb wrote:

> some explanation on why? , would be great .

In my opinion it’s a String vs. RegExp problem.

Your rdebug output said that your syntax variable was a String, or?:

> (rdb:1) v l  
> filename =\> “C:\rubypgm\Paramsyntax\prm.102”  
> newsettings =\> “1abcd”  
> param =\> “102”  
> syntax =\> “/^[0-1]{1}[a-zA-Z]{1,25}$/”

syntax has doublequotes and is a String here (maybe a hint or just  
output?).

However if you read from file you get a String not an Regexp you want to  
have a new Regexp so you have to use Regexp.new or /“string”/.

In a test the String don’t match like a Regexp:

irb\> syntax="/^[0-1]{1}[a-zA-Z]{1,25}$/"  
irb\> newsettings=“1abcd”  
irb\> syntax.match(newsettings) =\> nil  
irb\> syntax2=/^[0-1]{1}[a-zA-Z]{1,25}$/  
irb\> syntax2.match(newsettings) =\> #\<MatchData “1abcd”\>

you want:  
irb\> Regexp.new("/^[0-1]{1}[a-zA-Z]{1,25}$/") =\> //^[0-1]{1}[a-zA-Z]  
{1,25}$//

or what you did:

> /#{syntaxfile.gets.chomp}/.match(@newsettings.value)

It’s a Regexp so it works as expected.

hth. regards, Sandor  
Szücs
