How do I add open and close brackets for every line using Notepad++
I've tried to find the lines with this expression: (.+).
and then I replaced with (\1). It works for single quotes when I try to replace it as '\1'
But for () it is not working.
Thanks
You have to escape the parentheses in the replacement string too:
So replace ^(.+)$ with \(\1\).
Parentheses are special for Notepad++, and AdrianHHH has found the explanation:
It uses the boost regex flavor, which uses the following syntax for replacement strings: Boost-Extended Format String Syntax
The + within the regular expression means one or more of the previous item which is a . meaning any character. You should use the expression ^(.*)$ where the * means zero or more of the previous item. I like to add the ^ and $ to expressions to make it clear that I want the beginning and the end of the line, although in this case they are unnecessary.
The replacement text should have (as other answers indicate but do not explain) the round braces escaped. The replacement should be \(\1\). (Just checked in Notepad++ 6.6.7 and the doubled round braces ((\1)) previously stated in this answer does not work. Braces in the replacement string must be \( and \).)
Notepad++ replacement expressions can be complex, round braces introduce the variations. See Multiple word search and replace in notepad++ for one example and links to more documentation.
Related
I have a text file with a list of elements separated by line-breaks, like this:
alpha
beta
gamma
...
I want to get it into this format:
(alpha),
(beta),
(gamma),
...
So I am using following regular expressions in Notepad++ for replacing those lines:
Find: ([^\n]+)
Replace: \($1\),
but the output now strangely has another line-break for each line into it:
(alpha
),
(beta
),
(gamma
),
...
I have no clue how this is happening. When I solely use $1 or \), apart for replacement it works just fine, but everytime I put a literal after the backreference it puts a line-break in between. I know that I can work around that with another regular expression afterwards, but could anybody explain to me why exactly this is happening?
Instead of [^\n] (=any char but an LF, line feed, \n) you should use . that only matches any char other than line break chars. Use the following regex to match a non-empty line:
^.+$
Replace with \($0\), where $0 replacement backreference (also called placeholder) stands for the whole match and the parentheses are escaped (since parentheses are special metacharacters inside Boost replacement patterns used to define conditional replacement patterns).
No need to use the m modifier here since ^ and $ anchors match start and end of the line respectively by default in Notepad++.
See the NPP S&R settings:
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 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
I'm trying to use a regex search and replace to find and fix any unescaped quotation marks with escaped question marks. This is not in any particular language - just using regex to search and replace in Sublime Text 2.
I can find them just fine with this regex:
([a-zA-Z0-9!##$%^&*()_+=-\?><:;\/])\"
Trying to replace is giving me some headaches. I thought this would work:
$0\\\"
but it's adding an extra quote in (or leaving the previous one there somehow).
e.g.,
e"
becomes
e"\"
instead of just
e\"
What the hey? I can't seem to find a combination in the replacement that will work!
In the replacement $0 will be a reference to the entire match, including the quote. It looks like you should be using $1 instead which will be the first capturing group, so just the character immediately before the quote. So your replacement string would be "$1\\\"".
How to append and prepend a character at start and end of each file line?
I have this file structure:
140","Bosnia
160","Croatia
170","Serbia
180","Montenegro
200","Slovenia
What I need is to add a double quote " at the start and at the end of each file line, using regular expressions in Notepad++ editor.
Thanks!
Just search for
(.*)
and replace with
"\1"
with regular expression option activated. Regular expressions are working only on a row bases, so (.*) matches the complete row and because of the brackets around you can access the match using \1.
Try searching ^(.*)$ and replacing by "$1".
bye ;)
You can match the whole, even an empty line, with
^.*$
You can match a non-empty line with
^.+$
You may match a non-blank line with
^\h*\S.*$
Now, all you need to do to wrap these lines with any text of your choice, you need to use the backreference to the whole match (see Replace with whole match value using Notepad++ regex search and replace):
"$0"
"$&"
"$MATCH"
"${^MATCH}"
If you need to wrap the whole line with parentheses, you will need to escape them since ( and ) are "special" in the Notepad++ replacement pattern, \($&\).
Whenever you need to insert a backslash, make sure you double it, \\$&\\.