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

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

Related

Append End of Line with Substring from Current Line [duplicate]

This question already has an answer here:
Replace with whole match value using Notepad++ regex search and replace
(1 answer)
Closed 9 months ago.
I've scoured Stack Overflow for something just like this and can't seem to come up with a solution. I've got some text that looks like this:
command.Parameters.Add("#Id
command.Parameters.Add("#IsDeleted
command.Parameters.Add("#MasterRecordId
command.Parameters.Add("#Name
...
And I would like the text to end up like this:
command.Parameters.Add("#Id", acct.Id);
command.Parameters.Add("#IsDeleted", acct.IsDeleted);
command.Parameters.Add("#MasterRecordId", acct.MasterRecordId);
command.Parameters.Add("#Name", acct.Name);
...
As you can see, I essentially want to append the end of the line with: ", acct.<word between # and second ">);
I'm trying this:
Find What: (?<=#).+?(?=\r) - This works, it finds the appropriate word.
Replace: \1", acct.\1); - This doesn't. It changes the line to (for Id):
command.Parameters.Add("#", acct.
Not sure what I'm doing wrong. I thought that \1 is supposed to be the "capture" from the "Find what" box, but it's not I guess?
The \1 backreference will only work if you have a capturing group in your pattern:
(?<=#)(.+?)(?=\r)
If you're not using a capturing group, you should use $& instead of \1 as a backreference for the entire match. Additionally, parentheses in the replacement string need to be escaped. So, the replacement string should be:
$&", acct.$&\);
You might also want to use $ instead of the Lookahead (?=\r) in case the last line isn't followed by an EOL character.
Having said all that, I personally prefer to be more explicit/strict when doing regex substitution to avoid messing up other lines (i.e., false positives). So I would go with something like this:
Find: (\bcommand\.Parameters\.Add\("#)(\w+)$
Replace: \1\2", acct.\2\);
Note that \w will only match word characters, which is likely the desired behavior here. Feel free to replace it with a character class if you think your identifiers might have other characters.
You could also omit the lookbehind, and match the # and then use \K to clear the current match buffer.
Then you can match the rest of the line using .+
Note that you don't have to make the quantifier non greedy .*? as you are matching the rest of the line.
In the replacement, use the full match using $0
See a regex demo for the matches:
Find what:
#\K.+
Replace with:
$0", acct.$0\)
If there must be a newline to the right, you might also write the pattern as one of:
#\K.+(?=\r)
#\K.+(?=\R)

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

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.

Regex Multiline Capitalize all first letter of words and remove space notepad ++

I made this regex demo (working) here: https://regex101.com/r/WSwEbY/6
When I use it in notepad ++, it doesn't work with multiple lines:
hello ladies how are you Today
hello ladies how are you Today
-> result is on a single line:
helloLadiesHowAreYouTodayHelloLadiesHowAreYouToday
Informations:
search: [^\w]+(\w)
replaceby: \U$1
n++version: 7.5.8
I also try to check 'multiline' or add '$' to en of the search.
Here, you tried to match everything that is not a word character:
[^\w]
However, the new line character \n is also not a word character so it will also be matched by [^\w] and replaced.
You should exclude \n from the character class as well:
[^\w\n]+(\w)
Demo
How about matching just the space or the start(^) with multiline flag?
(?:^| +)(\w)
sub:
\U$1
In addition to not matching newlines in the repeated character set, you should also alternate with a check for if you're at the start of a line - that way the first word on a line will be capitalized as well. Use the m flag so that ^ matches the start of a line:
(?:^|[^\w\n]+)(\w)
Replace with:
\U$1
Output:
HelloLadiesHowAreYouToday
IAmFineThankYou
https://regex101.com/r/dsOcOD/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.