Fuzzy string matching?

Dmitry Olshansky dmitry.olsh at gmail.com
Sat Jul 16 12:51:02 PDT 2011


On 16.07.2011 10:17, dsmith wrote:
> Could you demonstrate how to use std.regex for pattern matching, preferably with a bool method?
>
> My usage of std.regex.match yields this error: core.exception.AssertError@/usr/include/d/dmd/phobos/std/regex.d(1796): 4294967295 .. 4294967295 vs. 5
>
> My usage is:     auto m = match(long_string, regex(str));
>                          writeln(m.hit);

Your and apparently an awful lot of people hit this, the thing is that 
.hit method is returning _matched slice_ of string if there is a match 
and asserts otherwise. (there is also issue of this assert having 
message is of a _very_ poor quality)
As it stands now regex works like ranges: you need to check if it was 
empty then use it, so if all you want to do is a test:

auto m = match(long_string, regex(str));
writeln(!m.empty); // substitute for "there was match"


Thinking more about this, it should be in synopsis part of std.regex in 
docs on d-p-l.org. Along with something like:

foreach(m; match("abc", regex("\w", "g")) //uses range syntax to iterate 
over all matches (so empty is checked)
     writeln(m.hit); // here m.hit is guaranteed to hold something (and 
not asserting)

>
> == Repost the article of Jonathan M Davis (jmdavisProg at gmx.com)
> == Posted at 2011/07/16 01:08 to digitalmars.D.learn
>
> On�Saturday�16�July�2011�05:07:38�dsmith�wrote:
>> �Until�recently,�you�could�easily�use�std.regexp.search(target_string,
>> �find_string),�but�regexp�is�apparently�no�longer�in�phobos.��I�seek�a
>> �simple�substitute.��std.algorithm.canFind�might�work,�as�it�is�bool.
>> �Maybe�try�something�like:
>>
>> �foreach(str;�strings)
>> �����foreach(fls;�system_files)
>> ���������if(std.algorithm.canFind(fls,�str))���//�usage�needs�verification
>>              str�~=�".ext";
> std.regex�is�std.regexp's�replacement.
>
> -�Jonathan�M�davis
>


-- 
Dmitry Olshansky



More information about the Digitalmars-d-learn mailing list