简体   繁体   English

AutoKey Python - 如何进行正则表达式替换

[英]AutoKey Python - How do I do Regular Expression Replacement

I'm using AutoKey , and I'm trying to figure out how to successfully perform regular expression pattern replacement within strings. 我正在使用AutoKey ,我正在试图弄清楚如何在字符串中成功执行正则表达式模式替换。 As a test, I saved a script that looks like this: 作为测试,我保存了一个如下所示的脚本:

NewStr := RegExReplace("abc123123", "123$", "xyz")
keyboard.send_keys(NewStr)

The script only returned a space, instead of the regex modified string I expected. 该脚本只返回一个空格,而不是我期望的正则表达式修改后的字符串。

I'm new to python scripting, and I got that code from the AutoHotKey tutorial, which is not the same is AutoKey, but AutoKey is probably modeled after AutoHotKey, so I thought I'd try it. 我是python脚本的新手,我从AutoHotKey教程获得了代码,这与AutoKey不同,但AutoKey可能是在AutoHotKey之后建模的,所以我想我会尝试一下。

The problem I keep having, is finding regular expression examples for AutoKey specifically. 我一直遇到的问题是专门为AutoKey找到正则表达式示例。 I'd appreciate any tool suggestions that will allow me to more efficiently write python scripts that play nice with AutoKey specifically. 我很感激任何工具建议,这将使我能够更有效地编写与AutoKey特别合适的python脚本。 Before now, I've never needed to learn python, but because AutoKey uses it for its scripting engine, I'm willing to learn it now. 在此之前,我从来不需要学习python,但由于AutoKey将它用于脚本引擎,我现在愿意学习它。

.sub(replacement, string[, count=0])

Returns the string obtained by replacing the leftmost non-overlapping occurrences of the RE in string by the replacement replacement. 返回通过替换替换替换字符串中RE的最左边非重叠出现而获得的字符串。 If the pattern isn't found, string is returned unchanged. 如果未找到模式,则返回字符串不变。

The optional argument count is the maximum number of pattern occurrences to be replaced; 可选参数count是要替换的模式最大出现次数; count must be a non-negative integer. count必须是非负整数。 The default value of 0 means to replace all occurrences. 默认值0表示替换所有出现次数。

Here's a simple example of using the sub() method. 这是使用sub()方法的简单示例。 It replaces colour names with the word colour: 它用颜色替换颜色名称:

>>> p = re.compile( '(blue|white|red)')
>>> p.sub( 'colour', 'blue socks and red shoes')
'colour socks and colour shoes'
>>> p.sub( 'colour', 'blue socks and red shoes', count=1)
'colour socks and red shoes'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM