Jump to content
Sign in to follow this  
Iczer

StringRegExpReplace() versus _ArrayUnique()

Recommended Posts

Iczer

Is it possible to get string with unique sub-strings using one-line StringRegExpReplace()  pattern? In other words to replace StringSplit() - _ArrayUnique() - _ArrayToString() construction with some RegExp one-liner?

#include <Array.au3>
;
$sSomeString = "asd|asd|qqq|www|qqq|ddd|ggg|jjj"
ConsoleWrite($sSomeString&@crlf)
;
$aTempArray = StringSplit($sSomeString,"|",2)
$aTempArray = _ArrayUnique($aTempArray)
$sSomeString = _ArrayToString($aTempArray,"|",1)
;
ConsoleWrite($sSomeString&@crlf)

Share this post


Link to post
Share on other sites
mikell

Yes, but the pattern construction depends a lot on the content of the substrings

$sSomeString = "bcd|as-d|as-d|-qqq|www|-qqq|ddd|ggg|jjj"
$ret = StringRegExpReplace($sSomeString, '(?<=^|\|)(.+\|)(?=.*\1.*)', '')
msgbox(0,"", $ret)
Edited by mikell

Share this post


Link to post
Share on other sites
jchd

Yes, like this:

$sSomeString = "asd|asd|qqq|www|qqq|ddd|ggg|jjj|jjj"
ConsoleWrite($sSomeString & @LF)
$sSomeString = StringRegExpReplace($sSomeString, "(\b[^|]+)(\|)(?=(?:[^|]+\|)*\1(?:\||$))", "")
ConsoleWrite($sSomeString & @LF)

mikell,

Beware of dot* and boundaries!

$sSomeString = "bcd|as-d|as-d|-qqq|www|something-qqq|ddd|ggg|jjj"
$ret = StringRegExpReplace($sSomeString, '(?<=^|\|)(.+\|)(?=.*\1.*)', '')
msgbox(0,"", $ret)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
mikell

jc,

That's precisely the reason why I wrote a warning about the substring content

Try your script on this string

$sSomeString = "bcd|as-d|as-d|-qqq|www|qqq|something-qqq|ddd|ggg|jjj"

:)

Edit

before writing a regex, you have to know exactly what you're trying to match (Jan Goyvaerts)

Edited by mikell

Share this post


Link to post
Share on other sites
jchd

The issue is not with the character class allowed but with subsequent prefixes of 1 capture:

$sSomeString = "asd|asd|qqq|www|qqq|ddd|ggg|asddd|jjj|jjj"
ConsoleWrite($sSomeString & @LF)
$sSomeString = StringRegExpReplace($sSomeString, "(\b[^|]+)(\|)(?=(?:[^|]+\|)*\1(?:\||$))", "")
ConsoleWrite($sSomeString & @LF)
$ret = StringRegExpReplace($sSomeString, '(?<=^|\|)(.+\|)(?=.*\1.*)', '')
ConsoleWrite($ret & @LF)

You discard "ddd|" because this capture as 1 also appears later in substring "asddd|" and matches the subpattern ".*1".

Of course the final pattern will have to cope with the actual allowable charset in real-wolrd substrings.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
mikell

I agree, but if the substring starts with a W then the word boundary will not work any more, regardless of the character class used

Edit

$sSomeString = "asd|asd|-qqq|www|-qqq|ddd|ggg|dddas|asddd|jjj|jjj"
$ret = StringRegExpReplace($sSomeString, '(?<=^|\|)(.+)(\|)(?=.*(?<=\|)\1(?=\||$))', '')
ConsoleWrite($ret & @LF)

:sweating:

Edited by mikell

Share this post


Link to post
Share on other sites
jchd

OK, but I was only adhering to the example format posted by the OP.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×