Help converting Notepad++ format to Pythonscript
-
sorry, didn’t understand that you want a python script solution with regular expression replacement.
regex = r'(\(.*?\))|(\w+)\s' editor.rereplace(regex, lambda m: '{}{}'.format(*m.groups()))Cheers
Claudia -
I am most-curious about why the OP (@Andrew-Clark) could get his regex replacement working in interactive N++ and not with Pythonscript, but perhaps that is a question that isn’t going to get answered…my guess would be not using raw string notation (the leading
rinr'regex') in the PS… -
or maybe it was about how to get the match groups returned as replacement … ??
Cheers
Claudia -
Hello @andrew-clark, @scott-sumner, @claudia-frank and All,
I was away this weekend, to hike in the Cevennes mountains and my feet still remember theses rides ! As you can imagine, just sitting, comfortably, in front of your laptop and discussing Notepad++ matters is a real treat ;-))
So, Andrew, as for me, I would use, with native N++, the regex S/R, below :
SEARCH
(?-s)(\(.+?\))|\x20REPLACE
?1\1
Notes :
-
As usual , the
(?-s)modifier means that any regex dot character (.) will match a single standard char., only. -
Then, this regex would match :
-
First, any shortest range of chars, between parentheses, stores it as group
1and rewrites this group as it is. ( Note that the(and)characters have to be escaped, with a\symbol, to be considered as literals ! ) -
Secondly, any space character, just ignored in replacement.
-
Cheers,
guy038
-
-
Hi Guy, nice shortening but one question - it seems that
?1is, for the posted example data, not needed in the replacement - what is the idea behind it?Thank you and cheers
Claudia -
Just in case someone wants to add this into a python script it would now look like this
regex = r'(?-s)(\(.+?\))|\x20' editor.rereplace(regex, lambda m: '{}'.format(m.groups()[0]))Cheers
Claudia -
@Claudia-Frank said:
or maybe it was about how to get the match groups returned as replacement … ??
I don’t know…the Pythonscript docs for
editor.rereplace()seem to have an adequate example of this…but considering it now maybe it could be made better? -
-
Hi, @andrew-clark, @scott-sumner, @claudia-frank and All
Sorry, for my late answer as I was elaborating a regex, for the tricky case, below :
https://notepad-plus-plus.org/community/topic/16053/how-to-ascend-numbers/3
Ah, how silly am I ! Of course, when the regex finds a space character ( the second alternative ), the group
1is not defined. Thus, in replacement, it simply rewrites…nothing, as group1represents a zero-length string ;-))Clever deduction, Claudia. So a possible regex is :
SEARCH
(?s)(\(.+?\))|\x20REPLACE
\1Note : I also changed the
(?-s)modifier into(?s), which allows possible multi-lines ranges of text, between parentheses !Best Regards,
guy038
-
Hi, @andrew-clark, @scott-sumner, @claudia-frank and All
Ah :-(( I should have tested my idea of changing
(?-s)into(?s), before posting. Actually, in case of multi-lines range of characters, between parentheses, the suitable regex S/R is , rather :SEARCH
(\([^()]+?\))|\x20REPLACE
\1Hope it’s the right one, this time :-D
Cheers
guy038
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login