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.
Related
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
Command below is from this answer: https://stackoverflow.com/a/208220/3470343
The command adds the word new. as a prefix. That's understood.
rename 's/(.*)$/new.$1/' original.filename
However, I'd like to ask why the open and close brackets are required here:
(.*)
And also why is $1 the variable which stores the original file name, why can't I do the the same with following (where i have replaced $1 with $2):
rename 's/(.*)$/new.$2/' original.filename
I'm still relatively new to bash, so help would be greatly appreciated.
First off, (.*)$ is what is known as a regular expression (or regex). Regular expressions are used to match text based on some rules.
For example, .* matches zero or more characters. $ matches the end of the line. Because regular expressions by default are greedy, .*$ matches the whole line (although, precisely because regexes are greedy, $ is superfluous).
However, I'd like to ask why the open and close brackets are required here: (.*)
The round brackets denote a group. Groups are used to "save" the contents of the matched text, so that you can use it later.
And also why is $1 the variable which stores the original file name, why can't I do the the same with following (where I have replaced $1 with $2): ...
In the case of rename(1), the first group is stored in $1, the second group is stored in $2 and so on. For example, the following regular expression:
(a)(b)(c)
stores a single a into $1, a single b into $2 and so on.
There you have only one group, therefore you must use $1. Tokens like $2, $3, ... will be empty.
Last but not least, you could use a shorter, equivalent command:
rename 's/^/new./'
Here ^ denotes the start of the string.
Here is my regex: [0-9]+ thd
It will find a number that has thd to the right of it. Now how do I replace it with the same exact value but with a space in front of it?
I tried \1 \2 $1 $2 but that just causes it to get deleted.
If my regex finds the number 389 THD, I just want it to add a space in front of it.
If I understand you correctly, you should use " $0" (without the quotes).
$0 is the entire match, $1 is the first capturing group, etc. Since there are no capturing groups in your regex, $1 and $2 are empty.
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).
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