Reading .txt File into String and Matching with RegEx

BoQsc vaidas.boqsc at gmail.com
Mon Dec 11 08:31:47 UTC 2023


On Monday, 11 December 2023 at 05:18:45 UTC, thinkunix wrote:
> BoQsc via Digitalmars-d-learn wrote:
>> This is something I've searched on the forum and couldn't find 
>> exact answer.
>> 
>> TLDR: `r"^."` is matching the very first two character in the 
>> `input` string.
>
> Don't you need two dots to match two characters?
> Each dot being the regex to match a single character,
> so `r"^.."` instead of `r"^."` to get the first two characters.
>
> When I run your program (on linux with rdmd from DMD 2.106.0), 
> I get:
>
> [["H"]]

Yeah, that's true, my mistake, forgot to update the snippet and 
note properly. Thanks!

```
import std.stdio : writeln;
import std.regex : matchAll;
import std.file  : read;

void main(){

	string input = cast(string)read("example.txt");
	writeln(matchAll(input, r"^.."));

}
```


More information about the Digitalmars-d-learn mailing list