Zach Rattner
Zach Rattner

Reputation: 21361

Can Notepad++ convert strings matched regular expression to lowercase?

I have a C++ source file that uses functions of the form getMemberName() to return member data. Instead, I'd like to use memberName().

In order to match instances of function names that need to change, I'm using the following regular expression:

(\s+)get([A-Z])

The problem is, I don't know how to replace the instance of \2 with its lowercase version. Does anyone have any ideas, or should I resort to writing a Perl script?

Thanks.

Upvotes: 5

Views: 5575

Answers (3)

Matt
Matt

Reputation: 74899

Note: This feature is available in Notepad++ via the \L\1\E regex substitution pattern.

  • \L lowercase on
  • \2 for matching group 2
  • \E lowercase off, in case you have any further replacements.

See Novices answer and the search page of Notepad++ manual for details.


There is, as usual, another way. This can be done in Notepad++ with the PythonScript plugin, as well as anything else a little beyond the scope of what's available in notepad++ without having to write a full plugin.

First, install the PythonScript plugin in Notepad++

Plugins > Plugin Manager > Show Plugin Manager

Check "Python Script" in the "Available" tab and click "Install" then restart Notepad++

Then setup a Python script

Plugins > Python Script > New Script

Give it a useful name

Add the following code

# Start a sequence of actions that is undone and redone as a unit. May be nested.
editor.beginUndoAction()

# trimFunctionName - for editor.pysearch
def trimFunctionName( index, match ):
    editor.pyreplace( match.re, match.group(1) + match.group(2).lower(), 1, 0, index, index )
    
# I couldn't work out how to jam the .lower call into a editor.pyreplace() 
# so used editor.pysearch() to check the regex and run a function with match 
# information
editor.pysearch(r'(\s+)get([A-Z])', trimFunctionName )

# end the undo sequence
editor.endUndoAction()

Then run the script.

Plugins > Python Script > Scripts > "yourScript"

You can prompt for user input or do a myriad of other things to scintilla with the provided objects

Upvotes: 11

Novice
Novice

Reputation: 81

Here is a very basic HTML tag converter (to lower case):

Left click Search->Replace (or CTRL H).

Enter the following in the appropriate fields:

Find what: <([^>]+?)>

Replace with: <\L\1>

Left click Replace All.

Done

Upvotes: 8

Andrew Redd
Andrew Redd

Reputation: 4692

Yes you should write a Perl script for that. The built in regex engine is not powerful enough to handle something like this.

Upvotes: 2

Related Questions