Searching for a string in a text buffer with a regular expression

Shammah Chancellor anonymous at coward.com
Fri Dec 6 03:47:01 PST 2013


On 2013-12-06 08:53:04 +0000, maxpat78 said:

> While porting a simple Python script to D, I found the following problem.
> 
> I need to read in some thousand of little text files and search every 
> one for a match with a given regular expression.
> 
> Obviously, the program can't (and it should not) be certain about the 
> encoding of each input file.
> 
> I initially used read() casting it with a cast(char[]), but, at some 
> point, the regex engine crashed with an exception: it encountered an 
> UTF-8 character it couldn't automatically decode. This is right, since 
> char[] is not byte[].
> 
> Now I'm casting with a Latin1String, since I know this is the right 
> encoding for the input buffers: and it works fine, at last... but what 
> about if I'd need to treat a RAW (binary? unknown encoding?) buffer?
> 
> Is there a simple and elegant solution in D for such case?
> Python didn't gave such problems!

Why don't you follow one of the file reading examples?

readText is what you're looking for.

http://dlang.org/phobos/std_file.html#.readText



More information about the Digitalmars-d-learn mailing list