NOTEPAD++ REGEX How do I replace and remove a whitespace from $0 - regex

I'm using this regex:
\s[0-9]+ thd
It finds what I want perfectly. I want to remove the white space at the beginning. What should I put in the replace field?

Change the search text to \s([0-9]+ thd) and then the replacement id \1 or $1 depending on the type of regex.

Find:
\s([0-9]+ thd)
Replace:
$1

So if you using Notepad++ everything in parenthesis refers to section build from \ and number in Replace box.
Look at example:
Find: (\s)([0-9]+ thd)
Replace: \1\2
This give you back first \1 and second \2 section - nothing unusual, but when you live only \2 after replace you'll get only part found by ([0-9]+ thd)
Going further if you split your expression to (\s)([0-9]+)(\s)(thd) then you'll get 4 parts
This parts will be represented by \1\2\3\4 and if you need add something more to your output line:
for example added_text_before\1added_text_after in Replace will result merging added text and section found - practice because there is no hidden magic out there.
Remember that using regex in Notepad++ have some limitations (to long expression aren't read).

Related

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.

How in notepad++ find/replace text between slashes?

How to find the text between the second and fourth slashes in a path like /folder/subfolder-1/subfolder-2/subfolder-3? I’m trying to replace this with something like /folder/new-folder/subfolder-3.
The most important for me is to be able to find the part after the n-th slash.
I tried the regex /((.*?)/){3}, but it doesn’t work.
Using Match resetter \K meta-character you are able to do it in a simpler way.
Find:
/.*?/\K(.*?/){2}
Replace with:
new-folder/
One way you could to it is by using this string in the pattern to replace
(/.+?)(/.+?){2}(/\S+)
And use this one in your pattern to replace it with
$1/new-folder$3
From your string:
/folder/subfolder-1/subfolder-2/subfolder-3
(/.+?) will match /folder as $1
(/.+?){2} will match /subfolder-1/subfolder-2 as $2 (not used)
(/\S+) will match everything that isn't a space, in this case/subfolder-3 as $3
Leaving you room to insert your new-folder in-between.
How can I just mark till the slash?
Find what: (/[^/]+/)[^/]+/[^/]+
Replace with: $1new-folder
To find text between second and forth slash you can use the regex ^(/[^/]*/)([^/]*/[^/]*) then you can reference to the text between slashes with \2 when replacing the text.
To keep the text before the slashes you can enter something like \1myNewTextBetweenSlashes2and4.
In notepad++ Find by this:
(/[^/]+)(?:/[^/]+/[^/]+/)(.*)
And Replace by this:
\1\/new-folder/\2
Make sure that: .matches newline is not checked
{2} indicates 2 levels after first level will be repalced by new-folder
Find:
(\/.*?\/)(.*?\/){2}(.*)
Replace:
$1new-folder/$3
Demo: https://regex101.com/r/XIA3IN/3

Notepad++ Regex replace Hyphens

I have a HTML and the alt tags are in this format:
alt="something-nice-and-beautiful"
How do I replace all hyphens - with spaces only between the alt tags?
I'm still wrapping my brain around this one. My first inclination is to say use this as the search
(alt="[^"]*?)\-(.*?")
and then replace with \1 \2. Each time you run it, it will replace one hyphen with one space, just keep doing Replace All until there are no matches left. I might come up with a more elegant solution if I think about it more but this works.
In the source expression (make sure you enable .* regular expression button). Since this isn't going to work with a single expression, I'd do this (assuming that at most your tags are of a manageable length of course!)
Search for:
(?<=alt=")(\w+)-(?=")
Replace with:
$1
Then search/replace again, adding a (\w+)-:
(?<=alt=")(\w+)-(\w+)-(?=")
$1 $2
And again:
(?<=alt=")(\w+)-(\w+)-(\w+)(?=")
$1 $2 $3
...
(?<=alt=")(\w+)-(\w+)-(\w+)-(\w+)(?=")
$1 $2 $3 $4
Adding additional words as you go to ensure all your tags are replaced.

NotePad++ Regular Expression and Replace

How can i replace
$lang['abc'] with lang('abc')
and
{$lang['abc']} with ".lang('abc')."
including quotes.
Where i am stuck is how RegExp can save 'abc' to be used in replaced text
First one (tested in N++):
Search: \$lang\['(abc)'\]
Replace: lang\('$1'\)
Second one (also tested in N++):
Search: \{\$lang\['(abc)'\]\}
Replace: ".lang\('$1'\)."
The back reference mentioned by devnull is the content of the (abc) parentheses. The parentheses capture abc into Group 1. That content is referred to in the replacement as "$1". You may like to read all about regex capture.

Regex using Notepad++ to add space before a capital letter

I have looked around and found good answers but none work with notepad++, most are for java and php. I have found the search strings below but obviously I'm a noob with regex as i don't know what open/close tags are proper in notepad++.
I would like to add a space before each capital letter.
Example:
StackOverflowKegger
becomes
Stack Overflow Kegger
This is what i have found.
Find: [a-z]+[A-Z]+
Replace: $1 (there is a space before the $)
Find:
(?<!^)((?<![:upper:])[:upper:]|[:upper:](?![:upper:]))
("(\\p{Ll})(\\p{Lu})","$1 $2")
(?!^)(?=[A-Z])
Any help would be appreciated.
Search string: (.)([A-Z])
Replacement: \1 \2
This doesn't insert spaces before capitals that are the first letter on their line.
In Notepad++, do a search-n-Replace (ctrl+h), in 'find what' input '([a-z])([A-Z])' without single quotes. in 'Replace with' input '\1 \2' without quotes.
Select radio button 'Regular Expression' and make sure you Check 'Match Case' checkbox. Now find next and keep replacing. it will convert camel or Pascal case strings into words with a space before every capital letter except the first.
Hope it is helpful. I just used it with one of my tasks.
Find: ^([A-Z])
Replace: \1
this will add a space to the first uppercase character in notepad++
Make sure you put the space before the \1 in the replace section.
WABET : <-from
WABET : <-to
Find what: .\K([A-Z])
Replace with: $1 a space before $1
Note!!!!!! Must to check match-case see in attached photo.
If you can live with a space before the first word, then this solution worked for me.
I used the following with the Regular Expression radio button checked.:
Find what: ([A-Z])
Replace With: \1
Note the leading space before the \1 in the replace