www.digitalmars.com         C & C++   DMDScript  

D - RegExp Behavior

reply Clint Olson <Clint_member pathlink.com> writes:
I don't understand the behavior of the program (with output) below.  All I'm
doing is testing all the filenames of the files in the current directory for an
expression I know is in several of the names.  Only one filename tests positive.
Can anyone tell me what is happening?

Here is the code (RegExpTest.d)
==================================
import std.file;
import std.regexp;

int main(char[][] args) {

char[] testExp = "Exp";
RegExp filenameRegExp = new RegExp(testExp, "m");
char[][] filenames = std.file.listdir(".");

printf("Testing filenames in current directory for: %.*s\n", testExp);

foreach ( char[] filename; filenames) {
int result = filenameRegExp.test(filename);
if (result) {
printf("  %d Filename match: %.*s\n", result, filename);
}
else {
printf("  %d Filename mis-match: %.*s\n", result, filename);
}
}

return 0;
}


Here is the (commented) output
========================
Testing filenames in current directory for: Exp
0 Filename mis-match: clean.bat
0 Filename mis-match: compile.bat
0 Filename mis-match: compile_test.bat
0 Filename mis-match: env.bat
0 Filename mis-match: GrepTree.d
0 Filename mis-match: GrepTree.java
0 Filename mis-match: GrepTree.obj
0 Filename mis-match: grep_tree.exe
0 Filename mis-match: grep_tree.map
0 Filename mis-match: RegExpTest.d      <-- should pass
1 Filename match: RegExpTest.exe
0 Filename mis-match: RegExpTest.map    <-- should pass
0 Filename mis-match: RegExpTest.obj    <-- should pass
0 Filename mis-match: test_data
0 Filename mis-match: wc.d
0 Filename mis-match: wc.exe
0 Filename mis-match: wc.map
0 Filename mis-match: wc.obj
Dec 19 2003
parent reply "Walter" <walter digitalmars.com> writes:
Try filenameRegExp.test(filename, 0)

"Clint Olson" <Clint_member pathlink.com> wrote in message
news:bs03ui$3015$1 digitaldaemon.com...
 I don't understand the behavior of the program (with output) below.  All
I'm
 doing is testing all the filenames of the files in the current directory
for an
 expression I know is in several of the names.  Only one filename tests
positive.
 Can anyone tell me what is happening?

 Here is the code (RegExpTest.d)
 ==================================
 import std.file;
 import std.regexp;

 int main(char[][] args) {

 char[] testExp = "Exp";
 RegExp filenameRegExp = new RegExp(testExp, "m");
 char[][] filenames = std.file.listdir(".");

 printf("Testing filenames in current directory for: %.*s\n", testExp);

 foreach ( char[] filename; filenames) {
 int result = filenameRegExp.test(filename);
 if (result) {
 printf("  %d Filename match: %.*s\n", result, filename);
 }
 else {
 printf("  %d Filename mis-match: %.*s\n", result, filename);
 }
 }

 return 0;
 }


 Here is the (commented) output
 ========================
 Testing filenames in current directory for: Exp
 0 Filename mis-match: clean.bat
 0 Filename mis-match: compile.bat
 0 Filename mis-match: compile_test.bat
 0 Filename mis-match: env.bat
 0 Filename mis-match: GrepTree.d
 0 Filename mis-match: GrepTree.java
 0 Filename mis-match: GrepTree.obj
 0 Filename mis-match: grep_tree.exe
 0 Filename mis-match: grep_tree.map
 0 Filename mis-match: RegExpTest.d      <-- should pass
 1 Filename match: RegExpTest.exe
 0 Filename mis-match: RegExpTest.map    <-- should pass
 0 Filename mis-match: RegExpTest.obj    <-- should pass
 0 Filename mis-match: test_data
 0 Filename mis-match: wc.d
 0 Filename mis-match: wc.exe
 0 Filename mis-match: wc.map
 0 Filename mis-match: wc.obj
Dec 19 2003
parent Clint Olson <Clint_member pathlink.com> writes:
Yup, fixed it.  Thanks!  I see in the source code what is going on.  Fixing the
docs to include that form of the test() method would be useful for future RegExp
users :)

In article <bs08b7$527$1 digitaldaemon.com>, Walter says...
Try filenameRegExp.test(filename, 0)

"Clint Olson" <Clint_member pathlink.com> wrote in message
news:bs03ui$3015$1 digitaldaemon.com...
 I don't understand the behavior of the program (with output) below.  All
I'm
 doing is testing all the filenames of the files in the current directory
for an
 expression I know is in several of the names.  Only one filename tests
positive.
 Can anyone tell me what is happening?

 Here is the code (RegExpTest.d)
 ==================================
 import std.file;
 import std.regexp;

 int main(char[][] args) {

 char[] testExp = "Exp";
 RegExp filenameRegExp = new RegExp(testExp, "m");
 char[][] filenames = std.file.listdir(".");

 printf("Testing filenames in current directory for: %.*s\n", testExp);

 foreach ( char[] filename; filenames) {
 int result = filenameRegExp.test(filename);
 if (result) {
 printf("  %d Filename match: %.*s\n", result, filename);
 }
 else {
 printf("  %d Filename mis-match: %.*s\n", result, filename);
 }
 }

 return 0;
 }


 Here is the (commented) output
 ========================
 Testing filenames in current directory for: Exp
 0 Filename mis-match: clean.bat
 0 Filename mis-match: compile.bat
 0 Filename mis-match: compile_test.bat
 0 Filename mis-match: env.bat
 0 Filename mis-match: GrepTree.d
 0 Filename mis-match: GrepTree.java
 0 Filename mis-match: GrepTree.obj
 0 Filename mis-match: grep_tree.exe
 0 Filename mis-match: grep_tree.map
 0 Filename mis-match: RegExpTest.d      <-- should pass
 1 Filename match: RegExpTest.exe
 0 Filename mis-match: RegExpTest.map    <-- should pass
 0 Filename mis-match: RegExpTest.obj    <-- should pass
 0 Filename mis-match: test_data
 0 Filename mis-match: wc.d
 0 Filename mis-match: wc.exe
 0 Filename mis-match: wc.map
 0 Filename mis-match: wc.obj
Dec 20 2003