I would like to take each line begin with 'modifier' until end of it and put them in list variable to sort it and replace it in the doc. So my regex is suppose to deal with each line and delete them after, I try to make this script:
varMatch = []
# suppose to fix the pos to first item
first_pos = editor.findText(0,0,editor.getLineCount(),'modifier')[0]
editor.gotoPos(first_pos)
def regFunc(m):
# fill the list with matching item
varMatch.append(m.group(0))
# define active line number
currLine = editor.lineFromPosition(editor.getCurrentPos())
# delete line
editor.deleteLine(currLine)
# Regex for searching each item
editor.research('^(modifier.*\n)', regFunc)
# sorting item
varMatch.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
# replace each sorted item in doc
for line in varMatch:
editor.addText(line)
That do sth but got lot of line in double or triple !!!
Sorry don't know what /S flag is...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
So something like this would do it.
If there is something unclear about the code , let me know.
The editor.beginUndoAction and editor.endUndoAction are used in
case something goes wrong terribly.
Unbelievable Claudia ! You really great, I could never find the way like that, and have to study it !
Maybe little misunderstood with : editor.addText('{}\r\n'.format(varMatch.pop()))
Thank you so much !
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
editor.addText does only add the text without the end of line characters (carriage retrun new line) therefore we need to add it. Format is used to format the string we want to add.
pop returns the last item of a list.
The script does the replacement from end of document to start of document.
Why?
Because if you would start from the beginning the position would change for the next
match. If we start from the end we don't have that issue.
Cheers
Claudia
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This doesn't work, why ?
With
regMatch = '(modifier.*)\n'
that work...!
What is expected?
The WHOLEDOC treats the ^ as start of doc and $ as end of document.
Do you use /S flag in addition?
Cheers
Claudia
I would like to take each line begin with 'modifier' until end of it and put them in list variable to sort it and replace it in the doc. So my regex is suppose to deal with each line and delete them after, I try to make this script:
That do sth but got lot of line in double or triple !!!
Sorry don't know what /S flag is...
Could you provide an example what you try to do?
Especially the part with sorting isn't clear to me.
Cheers
Claudia
Ii have doc file like that:
I need to sort (mean alphabetical sorting) only where line start with 'modifier'
and not move other line.
I have to did that on lot of files.
and the result should be this?
Written by somesoft 1.1.0
version v1.1.0
tags Name_tags
modifier baba sth 0.162000
modifier coco sth 0.398000
modifier etc.
modifier fifi sth -0.594000
modifier jojo sth 0.296000
modifier nono sth 0.318000
modifier popo sth 0.148000
modifier riri sth 0.238000
modifier titi sth 0.618000
modifier xoxo sth 0.372000
modifier zozo sth -0.644000
other word not begin by modifier
Cheers
Claudia
Last edit: CFrank 2017-03-03
yea that's right !
So something like this would do it.
If there is something unclear about the code , let me know.
The editor.beginUndoAction and editor.endUndoAction are used in
case something goes wrong terribly.
Unbelievable Claudia ! You really great, I could never find the way like that, and have to study it !
Maybe little misunderstood with :
editor.addText('{}\r\n'.format(varMatch.pop()))
Thank you so much !
editor.addText does only add the text without the end of line characters (carriage retrun new line) therefore we need to add it. Format is used to format the string we want to add.
pop returns the last item of a list.
The script does the replacement from end of document to start of document.
Why?
Because if you would start from the beginning the position would change for the next
match. If we start from the end we don't have that issue.
Cheers
Claudia