Domanda

I'm using AutoKey, and I'm trying to figure out how to successfully perform regular expression pattern replacement within strings. 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.

The problem I keep having, is finding regular expression examples for AutoKey specifically. I'd appreciate any tool suggestions that will allow me to more efficiently write python scripts that play nice with AutoKey specifically. 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.

È stato utile?

Soluzione

.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. 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 must be a non-negative integer. The default value of 0 means to replace all occurrences.

Here’s a simple example of using the sub() method. 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'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top