Menu

REGEX start line symbol doesn't work

Help
2017-03-01
2017-03-01
  • Marc Pechaud

    Marc Pechaud - 2017-03-01
    regMatch = '^(modifier.*)$'
    editor.research(regMatch, regFunc, editor.WHOLEDOC)
    

    This doesn't work, why ?

    WithregMatch = '(modifier.*)\n'
    that work...!

     
  • CFrank

    CFrank - 2017-03-01

    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

     
  • Marc Pechaud

    Marc Pechaud - 2017-03-02

    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...

     
    • CFrank

      CFrank - 2017-03-02

      Could you provide an example what you try to do?
      Especially the part with sorting isn't clear to me.

      Cheers
      Claudia

       
  • Marc Pechaud

    Marc Pechaud - 2017-03-03

    Ii have doc file like that:

    # Written by somesoft 1.1.0
    version v1.1.0
    tags Name_tags
    modifier baba sth 0.162000
    modifier fifi sth -0.594000
    modifier titi sth 0.618000
    modifier coco sth 0.398000
    modifier nono sth 0.318000
    modifier zozo sth -0.644000
    modifier xoxo sth 0.372000
    modifier jojo sth 0.296000
    modifier riri sth 0.238000
    modifier popo sth 0.148000
    modifier etc.
    
    other word not begin by modifier
    

    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.

     
  • CFrank

    CFrank - 2017-03-03

    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
  • Marc Pechaud

    Marc Pechaud - 2017-03-03

    yea that's right !

     
  • CFrank

    CFrank - 2017-03-03

    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.

    varMatch = []
    varMatchPos = []
    editor.beginUndoAction()
    def regFunc(m):  
        varMatch.append(m.group(0))
        varMatchPos.append(m.span())
    
    editor.research('^(modifier.*)', regFunc)
    
    varMatch.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
    
    for pos_start, pos_end in reversed(varMatchPos):
        editor.gotoPos(pos_start)
        currLine = editor.lineFromPosition(pos_start)
        editor.deleteLine(currLine)
        editor.addText('{}\r\n'.format(varMatch.pop()))
    editor.endUndoAction()   
    
     
  • Marc Pechaud

    Marc Pechaud - 2017-03-03

    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 !

     
  • CFrank

    CFrank - 2017-03-03

    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

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.