AHK Regex capture groups

I am trying to get RegExReplace to work for me, and I encounter a strange behavior:

MyString := "This is a little test. Why not?"

MyResult := RegExReplace(MyString, " (t.+)\.", "$1")

MsgBox, %MyResult%

This is the result:

Screenshot of a message box with the title 'RegexTest.ahk' displaying the text 'This is a littletest Why not?' with an 'OK' button.

This is the behavior I would have expected:

Screenshot of Trados Studio's regular expression tester with the expression '(t.+).' and the test string 'This is a little test. Why not?' Highlighted result shows 'test' with a note pointing to a space character indicating an issue with the regex.

AHK's regex replacement seems to include all characters into the match that are not explicitly excluded by being in the Regex but outside a capture group. (Space (" ") and period (".") in this example.) Is that normal? Can I turn this off and make it behave like all other Regex engines?

It's fine for simple regular expressions like the above, but once it gets really complex I don't want to re-test all my expressions to see whether they work in AHK as well...

Does anybody have any experience with this in AHK?

Daniel

EDIT: I should have added that this is the way to make it work: 

MyResult := RegExReplace(MyString, ".*(t.+)\..*", "$1")

I am not looking for the regular expression, I am wondering why AHK's Regex treatment is so unusual in this respect.



Generated Image Alt-Text
[edited by: Trados AI at 4:09 AM (GMT 0) on 5 Mar 2024]
emoji
Parents Reply Children