Replace ) in Notepad++ - regex

How do I replace ) when it comes after 1, 2, or 3 digits (not chars, and without removing the digit(s) themselves)?

Find what: ((?<!\d)\d{1,3})\)
Replace with: $1
This ensures that the ) comes after 1 to 3 digits (no more, no less).
Just append your replacement text to the end of $1. For example, if you want to replace it with the word TEST, your replacement would be $1TEST

As simple as:
Find: ((^|[^\d])\d{1,3})\)
Replace \1
And don't forget to enable te regular expressions in the panel.
Visit this link to try a working demo.

Related

How to encase words with quotations?

I am currently trying to convert a list of 1000 words into this format:
'known', 'buss', 'hello',
and so on.
The list i have is currently in this format:
known
worry
claim
tenuous
porter
I am trying to use notepad++ to do this, if anybody could point me in the correct direction, that would be great!
Use this if you want a comma delimited list but no extra comma at the end.
Ctrl+H
Find what: (\S+)(\s+)?
Replace with: '$1'(?2,:)
CHECK Wrap around
CHECK Regular expression
Replace all
Explanation:
(\S+) # group 1, 1 or more non spaces
(\s+)? # group 2, 1 or more spaces, optional
Replacement:
'$1' # content of group 1 enclosed in quotes
(?2,:) # if group 2 exists, add a comma, else, do nothing
Screen capture (before):
Screen capture (after):
How about replacing (\S+) with '$1'? Make sure your Regular Expression button is selected in the Find and Replace tool inside Notepad++
Explanation
(\S+) is regex for repeating non-whitespace characters (1 or more). Wrapping it in parenthesis puts it in a capture group which can be accessed in numerical order by using a dollar sign ($1).
'$1' will take that found text from the Find above and replace it with capture group #1 ($1) wrapped in single quotes '.
Sample
Input: known worry claim tenuous porter
Output: 'known' 'worry' 'claim' 'tenuous' 'porter'

How to use regex to remove semi colons?

Hello I have this kind of data :
a;b;c;d
1;2;3;4
a;g;h;j
f;g;f;d
a;d;8;d
And I would like to modify to have this :
a;bc;d
1;23;4
a;gh;j
f;gf;d
a;d8;d
Obviously I have a lot of lines but every time the semi colons are in the same position. I tried to select the columns with notepad++ and to to replace the semi colon by nothing but the box is grey...
Do you have a solution ?
Thank you !
Here is an online regex tester that i did the work on, you can just replace your data with the samples.
Regex : (\w+;)((\w+);(\w+))(;\w+)
DEMO
Hold ALT+SHIFT and use the arrow keys to select second semicolon and delete it.
OR
Hold ALT and click and drag the mouse to select a second semicolon and delete it.
OR
Find : ^(...)(.)
Replace with: \1
Ctrl+H
Find what: ^[^;]+;[^;]+\K;
Replace with: LEAVE EMPTY
check Wrap around
check Regular expression
Replace all
Explanation:
^ # beginning of string
[^;]+ # 1 or more non semicolon
; # 1 semi colon
[^;]+ # 1 or more non semicolon
\K # forget all we have seen until this position
; # 1 semi colon
Result for given example:
a;bc;d
1;23;4
a;gh;j
f;gf;d
a;d8;d
If the possible values of the data are lowercase characters a-z or a digit you could also capture the first 3 characters using a capturing group and a character class [a-z0-9] and after that match a semicolon. If there can be more than 1 character you could use a quantifier + for the character class like [a-z0-9]+
Then replace with the first capturing group.
Find what
^([a-z0-9];[a-z0-9]);
Replace with
$1
Regex demo
Or Using \K you could find ^[a-z0-9];[a-z0-9]\K; and leave Replace with empty.

Find and replace in N++

I have a N++ file with the following lines:
asm-java-2.0.0-lib
cib-slides-3.1.0
lib-hibernate-common-4.0.0-beta
I want to remove everything from the '-' before the numbers begin so the results look like:
asm-java
cib-slides
lib-hibernate-common
So far I've come up with [0-9]+ but that ignores the '.' and the trailing alphabets. Does anyone know a correct command for find and replace?
Ctrl+H
Find what: -\d.*$
Replace with: LEAVE EMPTY
check Wrap around
check Regular expression
UNCHECK . matches newline
Replace all
Explanation:
- # a dash
\d # a digit
.* # 0 or more any character but newline
$ # end of line
Result for given example:
asm-java
cib-slides
lib-hibernate-common
Use regex to find and replace
Find: ^(.+)-\d.*$
Replace: $1
Here's regex I used in VSCode to find and replace to get your task done:
(.*)?-\d.*
And replace with $1
Not sure about notepad++ but should get it done for you as well.

Find and replace with regex in Sublime Text 2

I am using Sublime Text 2 and I am wanting to shift my bootstrap2 project to bootstrap3.
How can I replace:
span2
to
col-lg-2
My pattern is
span[0-9]+
Your regular expression is correct, just place a capturing group around the part you want captured and then reference back to that group in your replacement.
Use Ctrl + H to open the Search and Replace, enable Regular Expression..
Find: span([0-9]+)
Replace: col-lg-$1
Edit
To add a class to your input field, you can use the following:
Find: <input[^>]*class="\K[^"]*
Replace: form-control
Sublime Text 2 allows you to use the \K escape sequence. \K resets the starting point of the reported match and any previously consumed characters are no longer included.
find what: span(\d)
replace with: col-lg-$1
(\d) matches one digit and stores it in match-group 1
$1 inserts the match-group 1
do not forget to enable the regular expression button (".*").
your "span([0-9])" is valid as well

Eclipse, regular expression search and replace

In eclipse, is it possible to use the matched search string as part of the replace string when performing a regular expression search and replace?
Basically, I want to replace all occurrences of
variableName.someMethod()
with:
((TypeName)variableName.someMethod())
Where variableName can be any variable name at all.
In sed I could use something like:
s/[a-zA-Z]+\.someMethod\(\)/((TypeName)&)/g
That is, & represents the matched search string. Is there something similar in Eclipse?
Thanks!
Yes, ( ) captures a group. You can use it again with $i where i is the i'th capture group.
So:
search: (\w+\.someMethod\(\))
replace: ((TypeName)$1)
Hint: Ctrl + Space in the textboxes gives you all kinds of suggestions for regular expression writing.
Using ...
search = (^.*import )(.*)(\(.*\):)
replace = $1$2
...replaces ...
from checks import checklist(_list):
...with...
from checks import checklist
Blocks in regex are delineated by parenthesis (which are not preceded by a "\")
(^.*import ) finds "from checks import " and loads it to $1 (eclipse starts counting at 1)
(.*) find the next "everything" until the next encountered "(" and loads it to $2. $2 stops at the "(" because of the next part (see next line below)
(\(.*\):) says "at the first encountered "(" after starting block $2...stop block $2 and start $3. $3 gets loaded with the "('any text'):" or, in the example, the "(_list):"
Then in the replace, just put the $1$2 to replace all three blocks with just the first two.
NomeN has answered correctly, but this answer wouldn't be of much use for beginners like me because we will have another problem to solve and we wouldn't know how to use RegEx in there. So I am adding a bit of explanation to this. The answer is
search: (\w+\\.someMethod\\(\\))
replace: ((TypeName)$1)
Here:
In search:
First and last (, ) depicts a group in regex
\w depicts words (alphanumeric + underscore)
+ depicts one or more (ie one or more of alphanumeric + underscore)
. is a special character which depicts any character (ie .+ means
one or more of any character). Because this is a special character
to depict a . we should give an escape character with it, ie \.
someMethod is given as it is to be searched.
The two parenthesis (, ) are given along with escape character
because they are special character which are used to depict a group
(we will discuss about group in next point)
In replace:
It is given ((TypeName)$1), here $1 depicts the
group. That is all the characters that are enclosed within the first
and last parenthesis (, ) in the search field
Also make sure you have checked the 'Regular expression' option in
find an replace box
At least at STS (SpringSource Tool Suite) groups are numbered starting form 0, so replace string will be
replace: ((TypeName)$0)
For someone who needs an explanation and an example of how to use a regxp in Eclipse. Here is my example illustrating the problem.
I want to rename
/download.mp4^lecture_id=271
to
/271.mp4
And there can be multiple of these.
Here is how it should be done.
Then hit find/replace button