| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
July 14, 2013 remove the ";" necessity in regex ? | ||||
|---|---|---|---|---|
| ||||
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
| ||||
July 14, 2013 Re: remove the ";" necessity in regex ? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Larry | what if you change your regex to: regex(`n=(hello|goodbye);?`) Also, would be appreciated if you could post these questions to d.learn. | |||
July 14, 2013 Re: remove the ";" necessity in regex ? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Larry | 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 | |||
July 15, 2013 Re: remove the ";" necessity in regex ? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | Ooops ! Of course ! I forgot this semicolon.. screwed mind. Many thanks, everything works fine. Bye ! | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply