www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - remove the ";" necessity in regex ?

reply "Larry" <deco33 hotmail.fr> writes:
Hello,

I would like to be able to make a regex from a text file :

[code]
version(Tango) extern (C) int printf(char *, ...);

import std.stdio;
import std.regex;
import std.file;
import std.format;

int main(char[][] args)
{
     string fl = readText("testregexd.txt");

     auto m = match(fl, regex(`n=(hello|goodbye);`));
     auto c = m.captures;

     writeln(c);

     return 0;
}
[/code]

But the main problem is that my file doesn't end with a semi 
colon ";". And so the regex cannot find anything in the file.

If I append this ";" at the end of my file, everything works as 
expected.

[code]
n=hello
[/code]
won't work whereas
[code]
n=hello;
[/code]
will.

Appending ";" with a mixin won't work either because it will 
create a new line.

Any idea ?

Thanks !

Larry
Jul 14 2013
next sibling parent David <d dav1d.de> writes:
what if you change your regex to:

regex(`n=(hello|goodbye);?`)


Also, would be appreciated if you could post these questions to d.learn.
Jul 14 2013
prev sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2013-07-14, 19:30, Larry wrote:

 Hello,

 I would like to be able to make a regex from a text file :

 [code]
 version(Tango) extern (C) int printf(char *, ...);

 import std.stdio;
 import std.regex;
 import std.file;
 import std.format;

 int main(char[][] args)
 {
      string fl = readText("testregexd.txt");

      auto m = match(fl, regex(`n=(hello|goodbye);`));
      auto c = m.captures;

      writeln(c);

      return 0;
 }
 [/code]

 But the main problem is that my file doesn't end with a semi colon ";".  
 And so the regex cannot find anything in the file.

 If I append this ";" at the end of my file, everything works as expected.

 [code]
 n=hello
 [/code]
 won't work whereas
 [code]
 n=hello;
 [/code]
 will.

 Appending ";" with a mixin won't work either because it will create a  
 new line.

 Any idea ?
I'm confused. You are using a regex with an explicit ; at the end, and you are surprised it only matches strings that end with ;? Step one: Remove the ; from the regex. Does that work? Step two (should step one fail): Add a $ where the ; was. Does that work? Step three (should step two fail): Give a better explanation of what the problem you're trying to solve is. (i.e. why is there a semicolon at the end of your regex?) -- Simen
Jul 14 2013
parent "Larry" <deco33 hotmail.fr> writes:
Ooops !

Of course !

I forgot this semicolon.. screwed mind.

Many thanks, everything works fine.

Bye !
Jul 15 2013