Skip to content

Commit 8719c65

Browse files
committed
Allow conditional replacements in rereplace
Search: (hello)|(world) Replace: (?1hi)(?2steve) Replaces hello world with hi steve, and world hello with steve hi. Test added to UTF8 Test case
1 parent d007434 commit 8719c65

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

PythonScript/python_tests/tests/ReplaceUTF8TestCase.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33
from Npp import *
44

55
class ReplaceUTF8TestCase(unittest.TestCase):
6-
def setUp(self):
7-
notepad.new()
8-
notepad.runMenuCommand("Encoding", "Encode in UTF-8")
9-
editor.write('Here is some text\r\nWith some umlauts XäXüXö\r\n');
10-
11-
def tearDown(self):
12-
editor.setSavePoint()
13-
notepad.close()
14-
15-
def test_simple_replace(self):
16-
editor.rereplace(r'some\s([a-z]+)', 'TEST');
17-
text = editor.getText()
18-
self.assertEqual(text, 'Here is TEST\r\nWith TEST XäXüXö\r\n');
19-
20-
def test_utf8_replace(self):
21-
editor.rereplace(r'X[äö]', 'YY');
22-
text = editor.getText()
23-
self.assertEqual(text, 'Here is some text\r\nWith some umlauts YYXüYY\r\n');
24-
25-
6+
def setUp(self):
7+
notepad.new()
8+
notepad.runMenuCommand("Encoding", "Encode in UTF-8")
9+
editor.write('Here is some text\r\nWith some umlauts XäXüXö\r\n');
10+
11+
def tearDown(self):
12+
editor.setSavePoint()
13+
notepad.close()
14+
15+
def test_simple_replace(self):
16+
editor.rereplace(r'some\s([a-z]+)', 'TEST');
17+
text = editor.getText()
18+
self.assertEqual(text, 'Here is TEST\r\nWith TEST XäXüXö\r\n');
19+
20+
def test_utf8_replace(self):
21+
editor.rereplace(r'X[äö]', 'YY');
22+
text = editor.getText()
23+
self.assertEqual(text, 'Here is some text\r\nWith some umlauts YYXüYY\r\n');
24+
25+
def test_replace_condition(self):
26+
editor.rereplace('(Here)|(Xä)', '(?1Cheese)(?2Y)')
27+
text = editor.getText()
28+
self.assertEqual(text, 'Cheese is some text\r\nWith some umlauts YXüXö\r\n')
2629
suite = unittest.TestLoader().loadTestsFromTestCase(ReplaceUTF8TestCase)

PythonScript/src/Replacer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ int BoostRegexMatch<CharTraitsT>::groupIndexFromName(const char *groupName)
138138
template <class CharTraitsT>
139139
void BoostRegexMatch<CharTraitsT>::expand(const char *format, char **result, int *resultLength)
140140
{
141-
CharTraitsT::string_type resultString = m_match->format(format);
141+
CharTraitsT::string_type resultString = m_match->format(format, boost::regex_constants::format_all);
142142

143143
std::string charResult(CharTraitsT::toCharString(resultString));
144144

0 commit comments

Comments
 (0)