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:
Related
I'm trying to find and replace some function calls in py program. The idea is to add some boolean parameter to each call found on the project.
I looked for solutions on the internet 'cause I don't know regex science at all... It seems like a basic exercice for regex guys but still.
In my case I have this call in a lot of files :
myFunction("test")
My gooal is to find and replace this call into :
myFunction("test", false)
Could you help me write the regex ?
Try this command:
sed -re 's/(myFunction)[[:space:]]*\([[:space:]]*("test")[[:space:]]*\)/\1(\2, false)/' SOURCE_FILENAME
If you prefer to replace the existing source file with an updated one, then write -i SOURCE_FILENAME instead of SOURCE_FILENAME.
This works by defining a pattern to match the function call you would like to update:
myFunction (obviously) matches the text myFunction;
[[:space:]] matches any whitespace character, mainly spaces and tabs.
[[:space:]]* matches zero or more whitespace characters.
\( and \) match literal parenthesis in your program text;
( and ) are regex metacharacters that match nothing, but ("test") matches "test" and captures the matched text for later use.
Note that this pattern captures two things using ( and ). The ("test") is the second of these.
Now let us examine the overall structure of the Sed command 's/.../.../'. The s means "substitute," so 's/.../.../' is Sed's substitution command.
Between the first and second slashes comes the pattern we have just discussed. Between the second and third slashes comes the replacement text Sed uses to replace the matched part of any line of your program text that matches the pattern. Within the replacement text, the \1 and \2 are backreferences that place the text earlier captured using ( and ).
So, there it is. Not only have I helped you to write the regex but have shown you how the regex works so that, next time, you can write your own.
Refer this:
import re
#Replace all white-space characters with the digit "9":
str = "The rain in Spain"
x = re.sub("\s", "9", str)
print(x)
you could use this regex to match and capture
(myFunction\("test")(\))
then use the regex below to replace
$1, false$2
I have a JSON file which has a bunch of jpg references that I'm trying to replace with png. I want to match on a pattern where there is a double digit and period before the jpg, capture 1, and use it in the replacement. The issue is I only ever get pattern not found.
"plith":"img/01.jpg"},{"block_ha....
where the substitution code looks like the following
:%s/\(\d{2}\.\)+jpg/$1png/g
I tried this substitution command:
:%s/\v(\d{2}\.)jpg/\1png/g
And it replaced the line:
"plith":"img/01.jpg"},{"block_ha....
With:
"plith":"img/01.png"},{"block_ha....
If the 2 digits and the following dot can be repeated, you can apply the + quantifier to \d{2}\.:
:%s/\v(\d{2}\.)+jpg/\1png/g
In your original command:
:%s/(\d{2}.)+jpg/$1png/g
There seemed to be 3 problems:
you use non-escaped parentheses to capture the digits, but by default you need to escape them. If you don't want to, you can switch to very magic mode by adding the atom \v in your pattern.
you don't escape the ., which means that it will match any character (except a newline), instead of a literal dot
in the replacement part, you use $1 to refer to the first capturing group, but it should be \1
I need to cut lines that have 6 or more characters, hyphen, then other characters or symbols. Hyphen and rest of line should be removed. Source text:
0402CS-2
0402CS-3
0402
7812-C
0603CS-1
0603CS-2
0603CS-3
As a result, I need this:
0402CS
0402CS
0402
7812-C
0603CS
0603CS
0603CS
To do that, I use Notepad++ regexp replace feature. Find pattern: ^([^\-]{6,})\-.+$ Replace pattern: \1
But there is no option "multiline", so, symbols "^" and "$" doesn't match ONLY beginning and end of the line and actually I have result:
0402CS
0402CS
0402
7812 <-- that's wrong!
0603CS
0603CS
0603CS
Please advice me how to fix find pattern? Or, maybe there is other handful and powerful free text editor that can do that?
^([^\n\-]{6,})\-.+$
^^
Just use \n as due to [^-] the regex can traverse to line below as use that line to make a match.
See demo.
https://regex101.com/r/BHO93c/1
for the input
0402
7812-C the regex matches both lines as 1 line and makes a match.
See demo if 0402 is not there.
https://regex101.com/r/BHO93c/2
That happens because the [^-] character class also matches a newline.
Add \n to it:
^([^\n-]{6,})-.+$
See the regex online demo (note the m multiline modifier (making ^ match the start of the line, and $ - the end of the line) and g modifier (enabling search for multiple occurrences) that is ON by default in Notepad++).
Note that escaping the hyphen is not necessary inside a character class when it is at the start/end of the class, and you never need to escape the hyphen outside the character class.
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.
I am attempting to edit a csv file, below is a sample line from this file.
|MIGRATE|;|10000|;|2ACC0003|;|30/09/13|;|Positive Adjmt.|;||;|MIGRATE|;|95004U
The beginning of the line |MIGRATE| needs to be modified without changing the second MIGRATE so the line would read
|MIGRATE|;|MIG_IN|;|10000|;|2ACC0003|;|30/09/13|;|Positive Adjmt.|;||;|MIGRATE|;|95004U
There are 7700 or so lines so if I am forced to do this manually I will probably cry a little.
Thanks in advance!
Just replace all the ones you want not changed with another word temporarily, then replace the rest with what you want. I'm not sure what you're asking here, but from what I can guess this might help.
It seems like you could just search for Just search for:
^\|MIGRATE\|
And replace with:
|MIGRATE|;|MIG_IN|
Make sure you've checked 'Regular expression' in the 'Search Mode' options.
Explanation: The ^ is a begin anchor; it will match the beginning of the line, ensuring that it does not match the second |MIGRATE|. The \ characters are required to escape the | characters since they normally have special meaning in regular expressions, and you want to match a literal |.
You can use beginning of line anchors:
Find:
^(\|MIGRATE\|)
Replace with:
$1;|MIG_IN|
regex101 demo
Just make sure that you are using the regular expression mode of the Search&Replace.
If you want to be a bit fancier, you can use a positive lookbehind:
Find:
(?<=^\|MIGRATE\|)
Replace with:
;|MIG_IN|
^ Will match only at the beginning of a line.
( ... ) is called a capture group, and will save the contents of the match in variable you can use (in the first regex, I accessed the variable using $1 in the replace. The first capture gets stored to $1, the second to $2, etc.)
| is a special character meaning 'or' in regex (to match a character or group of characters or another, e.g. a|b matches a or b. As such, you need to escape it with a backslash to make a regex match a literal |.
In my second regex, I used (?<= ... ) which is called a positive lookbehind. It makes sure that the part to be matched has what's inside before it. For instance, (?<=a)b matches a b only if it has an a before it. So that the b in ab matches but not in bb.
The website I linked also explains the details of the regex and you can try out some regex yourself!