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.
Related
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.
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.
I need a regex to replace this text:
("{scriptid}_*0123_00000000_ABC-Description*");
With this text:
("{scriptid}_*0123_00000000_ABC-Description*"));
Or in other words: *I have to add a second closing brace.
Explanation:
("{scriptid}_*0123_00000000_ABC-Description*");
("{scriptid}_4-digits_8-alphanumeric_X-alphanumeric-description");
I tried some expressions but it doesn't really work.
Could some one please help me?
A generic regex that matches all the lines ending with ');' (match also if there are trailing spaces) can be this:
s/\);\s*$/\)\);/
Test online here.
UPDATE: generic regex that adds a closing parenthesis, matches only lines starting with lr_start_transaction:
s/^\s*(lr_start_transaction.*\))\s*;\s*$/\1\);/
Test online here.
Anyway, if we are not talking about generic regex you have to also specify the language you are coding with (cause each regex engine is thinly different).
Consider also to include some lines that does not have to match.
Based on your initial string, you can select it with this:
/(\("\{scriptid\}_[0-9]{4}_[a-z0-9]{8}_[-a-z0-9]+"\));$/i
The $ matches the end of the line, and the i flag is for case-insensitive.
You can then replace with $1); (To simplify the replacement, I left the ; out of the capture group in the match.
http://refiddle.com/refiddles/5625058d75622d65a01c0000
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.
I want to replace the asterisks in a Markdown list with hyphens.
Example:
1.0
1.1
1.2
2
2.1
2.2
Currently I have a separate regex pattern for up to three levels of indentation set up in Keyboard Maestro for Mac:
I wonder if there isn't a smarter way to do this and which adresses all kinds of indentation.
In many regular expression search and replace systems, you can refer to a parenthesized group in the regular expression in the replacement, using \1, \2, etc. to refer to each successive group. So for example, in sed you could do:
sed -e 's/\(^[\t ]*\)\*/\1-/'
I'm not sure if Keyboard Maestro gives you that option. It mentions that it uses ICU regular expressions; if it also uses their replacement options, then you can use $1, $2 etc. to refer to the replacement.
If not, all is not lost. You can use a lookbehind assertion to match the sequence of whitespace before the the asterisk, without including the asterisk as part of the match; then just use a single dash as your replacement:
Search for: (?<=^[\t ]*)\*
Replace with: -
You can use submatching groups and reference them in the replacing string like this:
Regular expression matching your lines with list items: ([\t ]*)\*(.*)
The string used for replacement: \1-\2