NotePad++ Regular Expression and Replace - regex

How can i replace
$lang['abc'] with lang('abc')
and
{$lang['abc']} with ".lang('abc')."
including quotes.
Where i am stuck is how RegExp can save 'abc' to be used in replaced text

First one (tested in N++):
Search: \$lang\['(abc)'\]
Replace: lang\('$1'\)
Second one (also tested in N++):
Search: \{\$lang\['(abc)'\]\}
Replace: ".lang\('$1'\)."
The back reference mentioned by devnull is the content of the (abc) parentheses. The parentheses capture abc into Group 1. That content is referred to in the replacement as "$1". You may like to read all about regex capture.

Related

find and replace regex [duplicate]

I am using the Kate editor. Here is a minimal example that shows my problem:
I have a file with a bunch of occurrences of:
\command{stuff}
where stuff is some arbitrary string of letters. I want to replace this with
\disobey{stuff}
where stuff is unchanged. The regular expression:
\\command\{[a-zA-Z]*\}
matches such expressions. So I pull the replace dialog with CTRL-r, and enter
Find: \\command\{[a-zA-Z]*\}
Replace: \\disobey\{\1\}
So in the document, an actual instance is say,
\command{exchange}
and when I hit the replace button is changed to
\disobey{1}
In the Kate documentation: Appendix B: Regular Expressions, \1 should match the first pattern used. Is this indeed the correct syntax? I have also tried $1, #1, and various other things.
Here is a quote directly from the documentation:
The string \1 references the first sub pattern enclosed in parentheses
So you need to put [a-zA-Z]* in a capturing group, like ([a-zA-Z]*).
Find: \\command\{([a-zA-Z]*)\}
Replace: \\disobey\{\1\}
Wrap the value with ( ) to capture it as a group, so you can use it in your replace
So change your find regex like this:
\\command\{([a-zA-Z]*)\}
and you should do fine.

Notepad++ - RegEx - Number and Date

I have the following text in NotePad++, I am trying to write a RegEx to find and replace with the following.
Find: #,##/ (Look for the pattern 2,10/)
Replace: #\r (at the comma replace with a new line, so the date starts on a new line)
11/02/2016,18,54,61,13,37,05,2,10/29/2016,42,48,20,21,19,23,3,10/26/2016,48,56,02,16,03,24,2,10/22/2016,01,55,33,28,56,22,2,10/19/2016,43,63,16,38,10,23,2,10/15/2016,64,49,57,23,67,20,2,10/12/2016,34,44,30,16,37,16,2,
You seem to be looking for capturing groups: capture the different parts that should get split with a newline and use backreferences in the replacement pattern.
Search: (\d,)(\d+/)
Replace: $1\n$2
See the NPP screenshot:
This regex will work: (\d),(\d\d/)
Replace with: \1\r\2
Result example:

Find and replace parts of matched string in Notepad++

I have something in a text file that looks like '%r'%XXXX, where the XXXX represents some name at the end. Examples include '%r'%addR or '%r'%removeA. I can match these patterns using regex '%r'%\w+. What I would like to replace this with is '{!r}'.format(XXXX). Note that the name has to stay the same in the replace so I'd get '{!r}.format(addR) or '{!r}.format(removeA). Is there a way to replace parts of the matched string in this way while retaining the unknown variable name pulled out with \w+ in the regex search?
I'm specifically looking for a solution using the find and replace features in Notepad++.
You can use
'%r'%(\w+)
and replace with '{!r}.format\(\1\)
The '%r'%(\w+) pattern contains a pair of unescaped parentheses that create a capturing group. Inside the replacement pattern, we use a \1 backreference to restore that value.
NOTE: The ( and ) in the replacement must be escaped because otherwise they are treated as Boost conditional replacement pattern functional characters.
See more on capturing groups and backreferences.
Search on:
'%r'%(XXXX)
Replace with:
Whatever You like \1
\1 will match the first set of grouping parentheses.

How to replace Kate regular expression

I am using the Kate editor. Here is a minimal example that shows my problem:
I have a file with a bunch of occurrences of:
\command{stuff}
where stuff is some arbitrary string of letters. I want to replace this with
\disobey{stuff}
where stuff is unchanged. The regular expression:
\\command\{[a-zA-Z]*\}
matches such expressions. So I pull the replace dialog with CTRL-r, and enter
Find: \\command\{[a-zA-Z]*\}
Replace: \\disobey\{\1\}
So in the document, an actual instance is say,
\command{exchange}
and when I hit the replace button is changed to
\disobey{1}
In the Kate documentation: Appendix B: Regular Expressions, \1 should match the first pattern used. Is this indeed the correct syntax? I have also tried $1, #1, and various other things.
Here is a quote directly from the documentation:
The string \1 references the first sub pattern enclosed in parentheses
So you need to put [a-zA-Z]* in a capturing group, like ([a-zA-Z]*).
Find: \\command\{([a-zA-Z]*)\}
Replace: \\disobey\{\1\}
Wrap the value with ( ) to capture it as a group, so you can use it in your replace
So change your find regex like this:
\\command\{([a-zA-Z]*)\}
and you should do fine.

NOTEPAD++ REGEX How do I replace and remove a whitespace from $0

I'm using this regex:
\s[0-9]+ thd
It finds what I want perfectly. I want to remove the white space at the beginning. What should I put in the replace field?
Change the search text to \s([0-9]+ thd) and then the replacement id \1 or $1 depending on the type of regex.
Find:
\s([0-9]+ thd)
Replace:
$1
So if you using Notepad++ everything in parenthesis refers to section build from \ and number in Replace box.
Look at example:
Find: (\s)([0-9]+ thd)
Replace: \1\2
This give you back first \1 and second \2 section - nothing unusual, but when you live only \2 after replace you'll get only part found by ([0-9]+ thd)
Going further if you split your expression to (\s)([0-9]+)(\s)(thd) then you'll get 4 parts
This parts will be represented by \1\2\3\4 and if you need add something more to your output line:
for example added_text_before\1added_text_after in Replace will result merging added text and section found - practice because there is no hidden magic out there.
Remember that using regex in Notepad++ have some limitations (to long expression aren't read).