Find and replace in N++ - regex

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.

Related

Find lines without specified string and remove empty lines too

So, I know from this question how to find all the lines that don't contain a specific string. But it leaves a lot of empty newlines when I use it, for example, in a text editor substitution (Notepad++, Sublime, etc).
Is there a way to also remove the empty lines left behind by the substitution in the same regex or, as it's mentioned on the accepted answer, "this is not something regex ... should do"?
Example, based on the example from that question:
Input:
aahoho
bbhihi
cchaha
sshede
ddhudu
wwhada
hede
eehidi
Desired output:
sshede
hede
[edit-1]
Let's try this again: what I want is a way to use regex replace to remove everything that does not contain hede on the text editor. If I try .*hede.* it will find all hede:
But it will not remove. On a short file, this is easy to do manually, but the idea here is to replace on a larger file, with over 1000+ lines, but that would contain anywhere between 20-50 lines with the desired string.
If I use ^((?!hede).)*$ and replace it with nothing, I end up with empty lines:
I thought it was a simple question, for people with a better understanding of regex than me: can a single regex replace also remove those empty lines left behind?
An alternative try
Find what: ^(?!.*hede).*\s?
Replace with: nothing
Explanation:
^ # start of a line
(?!) # a Negative Lookahead
. # matches any character (except for line terminators)
* # matches the previous token between zero and unlimited times,
hede # matches the characters hede literally
\s # matches any whitespace character (equivalent to [\r\n\t\f\v ])
? # matches the previous token between zero and one times,
Using Notepad++.
Ctrl+H
Find what: ^((?!hede).)*(?:\R|\z)
Replace with: LEAVE EMPTY
CHECK Match case
CHECK Wrap around
CHECK Regular expression
UNCHECK . matches newline
Replace all
Explanation:
^ # beginning of line
((?!hede).)* # tempered greedy token, make sure we haven't hede in the line
(?:\R|\z) # non capture group, any kind of line break OR end of file
Screenshot (before):
Screenshot (after):
Have you tried:
.*hede.*
I don't know why you are doing an inverse search for this.
You can use sed like:
sed -e '/.*hede.*/!d' input.txt

How would you replace only a single character in the middle of text with duplicates?

How would you use the regex in Notepad++ to format replacing a single character that it finds in every line excepts for the duplicate ones in the certain line further?
test1:_|TEST:-TEST.|
test2:_|TEST:-TEST.|
test3:_|TEST:-TEST.|
As shown in the test code, there are two colons; I'm trying to replace the first colon with each line to a ; and NOT the second one found; the result of me doing the regex should equal to this:
test1;_|TEST:-TEST.|
test2;_|TEST:-TEST.|
test3;_|TEST:-TEST.|
Ctrl+H
Find what: ^.+?\K:
Replace with: ;
CHECK Wrap around
CHECK Regular expression
UNCHECK . matches newline
Replace all
Explanation:
^ # beginning of line
.+? # 1 or more any character but newline, not greedy
\K # forget all we have seen until this position
: # colon
Screen capture (before):
Screen capture (after):
I'm guessing that maybe this expression,
(\w+)\s*(?::)(\s*_\s*\|\s*\w+\s*:\s*-\w+\.\|)
with a replacement of $1;$2 might work.
DEMO 1
Or with less boundaries, this expression:
([^:]+):(.*)
with the same replace.
DEMO 2
It's done like this
Find (?m)^[^:\r\n]*\K:
Replace ;
https://regex101.com/r/rT1vG9/1

Regular expression Notepad++

Good Morning in my timezone
I want to replace a character that is in the beginning of each line
So i had used the following regular expression to find the text
^\d
And it works fine in finding all the characters
The problem is in the replace with
I want to replace with single quote followed by the same character found above
How can i do it ?
Thanks in advance
You may try this option:
Find:
^(?=\d)
Replace:
' <-- just a single quote
The find pattern uses a positive lookahead which asserts that the first character is a digit, but nothing is ever matched. Then, the replacement is a single quote.
You may use
Find What: ^\d
Replace With: '$0
where $0 is the backreference to the match value.
Another one would be:
Find:
^(\d)
Replace:
'\1
In this example \1 would be 1st captured group.

Keep only the first word of each line in notepad++ [duplicate]

This question already has answers here:
RegEx: Delete everything after the first word to the next line
(4 answers)
Closed 3 years ago.
For example I have:
hello I am ...
For the reason ...
Not sure if ...
I would like to keep the hello and For and Not and get rid of the rest in each line in notepad++.
Find:
^([^\s]*)\s.*$
Replace:
\1
Explanation:
^ start of line
([^\s]*) match and capture every non whitespace character up until
\s the first whitespace character
.* consume remainder of line until reaching the
$ end of line
Set the search/replace to regular expression mode, then search for
^(\w+).* (capture as many word-characters as possible at the beginning of each line)
and replace ALL with
$1 (the captured word)
Also, make sure ". matches newline" is off
Depending of what is a word for you, there're different solutions:
If a word is only alphabetic character
Find what: ^([a-zA-Z]+).*$
replace with: $1
If you want to match letters from any language:
Find what: ^(\p{L}+).*$
replace with: $1
If a word is any "word character" ie. [a-zA-Z0-9_]:
Find what: ^(\w+).*$
replace with: $1
If a word is any non whitespace character:
Find what: ^(\S+).*$
replace with: $1
Make sure Regular expression is checked but NOT . matches newline
Then click Replace all
CTRL+H, use Perl Regex, replace
^(\w+)\s.*$
to
$1

Remove Numbers in Notepad++

I have here a text file
with
245. asdasd
45. asdasd
42. gfhfgh
5353. sdfsdf
want to remove all numbers in front of it.
try it already with find and replace [0-9].
Ctrl+F - Replace - Search mode: regular expression
Find What: [0-9]+
Replace With:
// Replace with is empty
Ctrl-H - shows replace.
Ensure that 'regular expression' is on.
Use ^[0-9]+
Note begging ^ used to mark begin of line. Remove it if need remove all numbers
use RegEx search (should be ^\d+ for numbers with more than 1 digit):
On the replace tab switch to regular expressions; Search for ^\d+\.\s and leave Replace With empty.
(Matches start of line, any number, a dot, trailing space)
You should try to use this regular expression to remove what you want in the replace dialog:
^[0-9]*\.\ (.*)
And replace with:
\1
To search for digits:
Find what: [[:d:]]
Select: o Regular Expression
To remove only "line numbers with dots" use
[0-9]+.
To remove digits only from start of string
Input : 123. asdasd 3434
Output: . asdasd 3434
Replace All using below regex with o or more spaces in start of string
^\s*\d+
For Numbers
Ctrl H
Find what: [0-9]
Replace With:
(leave replace with blank)
For Period
Ctrl H
Find what: .
Replace With:
(leave replace with blank)