Southperry.net
Regex - Is this possible? - Printable Version

+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58)
+--- Thread: Regex - Is this possible? (/showthread.php?tid=33376)



Regex - Is this possible? - Fiel - 2010-11-27

Desired Functionality:
Using python regular expressions (perl-like), is it possible to create a query which only finds matches for which it has not already found?

Examples:

Input String:
1 1 1 2 2 2 3 3 3 4 4 4 40 40 40 45 45 45

Regex:
????

Expected Output:
['1', '2', '3', '4', '40', '45']

---------------------------------

Input String:
the the quick quick brown brown fox fox jumped jumped over over the the lazy lazy dog dog

Regex:
????

Expected Output:
['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'lazy', 'dog']

---------------------------------

Input String:
dog lazy the over jumped fox brown quick the the quick brown fox jumped over the lazy dog

Regex:
????

Expected Output:
['dog', 'lazy', 'the', 'over', 'jumped', 'fox', 'brown', 'quick']


Regex - Is this possible? - Dusk - 2010-11-27

Do the words have to be in succession to count as repeats? Yes : No


Regex - Is this possible? - Fiel - 2010-11-27

No.

Input:
1 2 3 4 5 3 2 4 5 1

Regex:
????

Output:
['1', '2', '3', '4', '5']

That answer your question?


Regex - Is this possible? - Dusk - 2010-11-27

(\w+\s)\1

finds duplicate words in succession, but you have to call the regex in a loop until it doesn't find any more duplicates.

Haven't tested it but you could try the following with a loop:

(\w+\s).*\1


Regex - Is this possible? - Russt - 2010-11-27

If you're not restricted to the regex itself, what you could do is iterate through all matches, adding them to a set. Then convert it to a list. Does not preserve order, though.

A less efficient method that does preserve order would just check if the match is in the list (linear time) before adding it.


Regex - Is this possible? - Fiel - 2010-11-27

You'd think a powerful parser like regex would be able to return only unique strings. I guess there's no management of that. Sad

I know I could do...

list(set((\d+\s)))

But I was just wondering if there was a way to do this just through regex.


Regex - Is this possible? - Russt - 2010-11-27

There may be; I don't know it all that well (more familiar with the, less featureful Lua pattern matching). Question is, why would you need one?


Regex - Is this possible? - Fiel - 2010-11-27

Because regex is in C and therefore runs much faster than python methods of doing it.


Regex - Is this possible? - Dusk - 2010-11-28

I haven't used C in a long time but for doing unique word counts in Perl, the easiest and fastest way to do it would be with a hash table. I forget how those are implemented in C.


Regex - Is this possible? - Russt - 2010-11-28

Python dictionary is basically a hash table, and a set is basically a dictionary that only holds empty values, so yeah.

And regex is written in regex. (That or Python is written in C, take your pick.) Either way you'd still need some way to represent a set, or in the absence of one you'd check previous values by doing a linear traversal which is even worse.


Regex - Is this possible? - Nikkey - 2010-11-28

Wouldn't tokenizing while reading through the string and putting them in a hash-table work fine?


Regex - Is this possible? - Fiel - 2010-11-28

Creating a lexical analyzer is way out of the scope of my project. I'll just work with what I have. Thanks!


Regex - Is this possible? - Spaz - 2010-11-29

Russt Wrote:If you're not restricted to the regex itself, what you could do is iterate through all matches, adding them to a set. Then convert it to a list. Does not preserve order, though.
This is the best way of doing it. If you need to preserve order, use an ordered set data structure.

Fiel Wrote:You'd think a powerful parser like regex would be able to return only unique strings. I guess there's no management of that. Sad

I know I could do...

list(set((\d+\s)))

But I was just wondering if there was a way to do this just through regex.

Russt Wrote:There may be; I don't know it all that well (more familiar with the, less featureful Lua pattern matching). Question is, why would you need one?

Fiel Wrote:Because regex is in C and therefore runs much faster than python methods of doing it.

Iterating through the matches and inserting each match into a set will not take much more time than the regex alone. Go ahead, try it.

[Image: premature_optimization.jpg]


Regex - Is this possible? - Fiel - 2010-11-29

Why is it premature optimization to attempt to use a well-established regular expression parser than to create my own method? I don't see the resemblance. All programmers ought to look for already present standardized implementations before attempting to implement their own solutions to prevent reinventing the wheel.


Regex - Is this possible? - GummyBear - 2010-12-07

I think the "wheel" of this problem is to do a unique sort on the list, which you will probably find library functions available in C to do this task.