开发者

Regular expression algorithm for integer lists

开发者 https://www.devze.com 2023-01-07 01:08 出处:网络
Usually, regular expression works for ASCII code. Say \"abbbd\".match(\"ab*d\"). I wonder if there exist algo开发者_如何学Gorithms or tools that allow user to match regular expression

Usually, regular expression works for ASCII code. Say "abbbd".match("ab*d").

I wonder if there exist algo开发者_如何学Gorithms or tools that allow user to match regular expression for integer lists.

e.g:

int[] a = {1,2,2,2,2,5}; 
a.match("12*5"); 

Thank you so much.


I've done something like that before, though I had to basically write my own engine for it. There's nothing magic about ASCII (or Unicode or any other character set), and when they teach regular expressions in school they usually use a tiny set of arbitrary symbols (like Σ = {a, b}) to keep things simple. The algorithms still work the same.

Most of the features of Perl-style regex engines are specific to characters. Some features like ^ and $ still work fine. Some like [:alnum:] make no sense at all. And others like [3-5] can be adapted to work with non-character strings.

One tricky bit (already noted by polygenelubricants and others) is that Perl regexes work well because the thing you're using to describe the language, and the thing you're matching, are both character strings -- the syntax doesn't work nearly as well for non-character-string alphabets. So /[3-5]/ in characters might need to be [3,4,5] (a list of integers), and so you need to build the language from expressions, rather than strings (unless you want to write your own parser!).

Why aren't most regex libraries generic on alphabet? Beats me -- it's a tremendously useful tool, and seems a terrible waste to apply it only to character strings. LINQ is nice but I'm not sure how it would help here.


I doubt it, mostly because it is so ambiguous. Just looking at the example that you provided, do you mean to match this:

{1, 2, 2, 2, 2, 5}

or this:

{12, ..., 5}

Sure, you could possibly improve the syntax slightly to fix this, but you would likely end up with a very messy syntax.

It would be just too complicated, and I'm sure that there are much better ways of doing it (list comprehensions, LINQ, etc).


You can use something like marge(), where marge will just make a string/character sequence having all members of an array-

a.marge().match("12*5");


int[] a = {1,2,2,2,2,5}; 
a.match("12*5"); 

Assume that you are trying to match "122225" against regular expression "12*5". Generate string from in using snprintf in C/C++ or .toString() in Java etc. should be clean and simple.

Not recommend you to get a special algorithm or tool for this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号