开发者

Regex OR in notepad++

I'd like to find all following instances in notepad++

action421
action732
action983

but ignore all other actionXXX combinations.

I'm looking for a regex similar to action(421)|(732)|(983) but it doesn't work. What regex would?

Does notepad++ regex engine 开发者_JAVA百科even have an OR operator?


Looks like Don Ho added the RegEx OR | operator to the Notepad++ native Find in the
latest version of Notepad (ver. 6.1.1).
Download Notepad 6.1.1 from here

Install Notepad 6.1.1, then go to Search->Find
Find what: action(421|732|983)
Search Mode: (*) Regular Expression

Click on the Find Next button


It is now possible in Notepad >= 10.1.1. According to Dragos Toader: "The latest version as of today (Notepad 10.1.1) supports | in RegEx"

== original answer below ==

It's not possible in Notepad++. See this answer.

But your regex is not correct. This one would work: action(421|732|983) if Notepad++ supported the | in regex.


You can use the regex helper plugin for Notepad++, but that is more for regex development than replace, but if you just want to find, I think this plugin can be helpful.

Regex OR in notepad++


Notepad++ doesn't support the alternation operator|. You can try the textfx ctrl+R regexp option but I think the same is true.


Use the PythonScript Notepad++ plugin for Python Regular Expression search function.
See here for functions

Editor.pysearch(expression, function[, flags[, startLine[, endLine]]])

or

Editor.pymlsearch(expression, function[, flags[, startPosition[, endPosition]]])  

Here's a simple program using the python Regular Expression search function editor.pysearch()
I've left lots of debug code in so you can see what is happening during the course of the function run.

# $Revision: 1.3 $
# $Author: dot $
# $Date: 2012/04/19 00:03:26 $

from Npp import *
import re, string

expression = notepad.prompt(
                 "Enter the RegEx search string then press Enter",
                 "RegEx and Search" ,
                 "")

debug = True
#debug = False

if debug:
    bufferID = notepad.getCurrentBufferID()

if debug:
    # Show console for debugging
    console.clear()
    console.show()

if expression != None:
    expressionSansNl = expression.replace('\r\n', '')

    if debug:
        console.write( expression + "\n" )

    # First we'll start an undo action, then Ctrl-Z will undo the actions of the whole script
    editor.beginUndoAction()

    if debug:
        console.write( 'editor.pysearch( r"%s" % expressionSansNl, None)\n' )

    editor.pysearch( r"%s" % expressionSansNl, None)

    # End the undo action, so Ctrl-Z will undo the above two actions
    editor.endUndoAction()

# Debug
if debug:
    notepad.activateBufferID(bufferID)

Map this script to Notepad++ shortcut Ctrl+<ChooseALetter> and run it.
Type in search RegEx action(421|732|983)
According to Python re documentation here, the | RegEx operator is supported.

I haven't tested this -- maybe the None parameter in the pysearch() function needs
to be a function that will select the resulting string. According to to docs here,
the function should return a python MatchObject. I think there's a way to get the
matching string from the MatchObject and select the next appearance of that matching
string in the editor object. I'll post a new revision of the answer once I'm done coding
and testing this.


your regex seems to be correct. but try to use this:

action(421|732|983)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜