Sign in to follow this  
Followers 0
eltorro

Regular expressions replace problem

6 posts in this topic

#1 ·  Posted (edited)

I have some text data that I need to replace some capitalized escaped sequences with lower case. I have the regular expression that finds the matches, but trying to use it with replace is causing some trouble.

Basically I need to match items like \N , %S or %D and replace them with \n %s or %d respectively.

If it was just these 3, it would be no problem to use StringReplace(), but there are more.

Local $line = "This is one line \Nfor of %S\t isn't that great?  There are %03d more lines left\n"
Local $sToTrans = ""
$sToTrans = StringRegExp($line,"[\\|%][\.[(0-9)]+[a-zA-Z]|[\\|%][a-zA-Z]",3)
If IsArray($sToTrans) Then
    For $x =0  to UBound($sToTrans)-1
ConsoleWrite($sToTrans[$x]&@lf)
    Next
EndIf
$sToTrans = StringRegExpReplace($line,"[\\|%][\.[(0-9)]+[a-zA-Z]|[\\|%][a-zA-Z]*",StringLower("\0"))
ConsoleWrite (@error &@LF)
ConsoleWrite($sToTrans&@lf)
$sToTrans = StringRegExpReplace($line,"[\\|%][\.[(0-9)]+[a-zA-Z]|[\\|%][a-zA-Z]",StringLower("$0"))
ConsoleWrite($sToTrans&@lf)

Any Ideas?

edit: minor grammatical change.

Edited by eltorro

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I have some text data that I need to replace some capitalized escaped sequences with lower case. I have the regular expression that finds the matches, but trying to use it with replace is causing some trouble.

Basically I need to match items like \N , %S or %D and replace them with \n %s or %d respectively.

If it was just these 3, it would be no problem to use StringReplace(), but there are more.

Local $line = "This is one line \Nfor of %S\t isn't that great?  There are %03d more lines left\n"
Local $sToTrans = ""
$sToTrans = StringRegExp($line,"[\\|%][\.[(0-9)]+[a-zA-Z]|[\\|%][a-zA-Z]",3)
If IsArray($sToTrans) Then
    For $x =0  to UBound($sToTrans)-1
ConsoleWrite($sToTrans[$x]&@lf)
    Next
EndIf
$sToTrans = StringRegExpReplace($line,"[\\|%][\.[(0-9)]+[a-zA-Z]|[\\|%][a-zA-Z]*",StringLower("\0"))
ConsoleWrite (@error &@LF)
ConsoleWrite($sToTrans&@lf)
$sToTrans = StringRegExpReplace($line,"[\\|%][\.[(0-9)]+[a-zA-Z]|[\\|%][a-zA-Z]",StringLower("$0"))
ConsoleWrite($sToTrans&@lf)

Any Ideas?

edit: minor grammatical change.

Having a hard time understanding the logic here... StringLower isn't passed along with StringRegExpReplace()...

Edit:

I'd just do this personally:

Local $line = "This is one line \Nfor of %S\t isn't that great?  There are %03d more lines left\n"
Local $aSRE, $sToTrans = $line, $i
$aSRE = StringRegExp($line, "(?:%[0-9a-z]+|\\\w|%\w)", 3)
For $i = 0 To UBound($aSRE) - 1
    $sToTrans = StringReplace($sToTrans, $aSRE[$i], StringLower($aSRE[$i]))
Next
ConsoleWrite($sToTrans&@lf)
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Having a hard time understanding the logic here... StringLower isn't passed along with StringRegExpReplace()...

I'm not clear on this from the help file for StringRegExpReplace.

"The text to replace the regular expression matching text with. To insert matched group text, \0 - \9 (or $0 - $9) can be used as back-references."

Edited by eltorro

Share this post


Link to post
Share on other sites

I'm not clear on this from the help file for StringRegExpReplace.

"The text to replace the regular expression matching text with. To insert matched group text, \0 - \9 (or $0 - $9) can be used as back-references."

Yes, they can be, but you're doing multiple replace types, and you don't know which one it's returning at the moment.
Local $sString, $i
$sString = "This is a o"
ConsoleWrite(StringRegExpReplace($sString, "(?i)a", "\0n") & @CRLF);Insert the char after
ConsoleWrite(StringRegExpReplace($sString, "(?i)a", "\1n") & @CRLF);Replace the char (since it's only 1 char).. The same as just "(?i)a", "n" in this case
In this case, we know what to replace with.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Thanks for the quick lesson.

I had the assumption that since it would return a match, "$0", that there might be the possibility to perform an operation on it.

Once again

Thanks!

Share this post


Link to post
Share on other sites

I'm not clear on this from the help file for StringRegExpReplace.

"The text to replace the regular expression matching text with. To insert matched group text, \0 - \9 (or $0 - $9) can be used as back-references."

The problem is that the back-reference is only evaluated in the RegExp function.

Using an example:

StringRegExpReplace('Hello', '(.*)', StringLower('\0'))

StringLower only receives '\0', so performs a case lowering operation on that string (which is irrelevant for those characters of course) and returns it.

StringRegExpReplace then receives '\0' from StringLower, and StringRegExpReplace turns it into 'Hello'. At the moment, there is no way to dynamically alter the data

In PHP there is a function set aside for performing functions on regular expression matches (called preg_replace_callback, for anyone curious), I've thought about requesting a similar function in AutoIt for a while but keep putting it off because I've been able to work around my issues in other ways. It basically works that instead of a replace string, you'd specify a function, which would receive an array of the matches, and you could do with them whatever you wanted.

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  
Followers 0