Reputation: 3
I need help with RegEx I just can't figure it out I need to search for broken Hashtags which have an space.
So the strings are for Example:
#ThisIsaHashtagWith Space
But there could also be the Words "With Space" which I don't want to replace.
So important is that the String starts with "#" then any character and then the words "With Space" which I want to replace to "WithSpace" to repair the Hashtags.
I have a Document with 10k of this broken Hashtags and I'm kind of trying the whole day without success.
I have tried on regex101.com
with following RegEx:
^#+(?:.*?)+(With Space)
Even I think it works on regex101.com it doesn't in Notepad++
Any help is appreciated.
Thanks a lot. BR
Upvotes: 0
Views: 585
Reputation: 374
Try this? (#.*)( )
.
I tried this in Notepad++ and you should be able to just replace all with $1
. Make sure you set the find mode to regular expressions first.
const str = "#ThisIsAHashtagWith Space";
console.log(str.replace(/(#.*)( )/g, "$1"));
Upvotes: 0
Reputation: 163632
In your current regex you match a #
and then any character and in a capturing group match (With Space)
.
You could change the capturing group to capture the first part of the match.
Then you could use that group in the replacement:
$1WithSpace
As an alternative you could first match a single #
followed by zero or more times any character non greedy .*?
and then use \K
to reset the starting point of the reported match.
Then match With Space
.
In the replacement use WithSpace
If you want to match one or more times #
you could use a quantifier +
. If the match should start at the beginning of string you could use an anchor ^
at the start of the regex.
Upvotes: 1
Reputation: 3670
Try using ^(#.+?)(With\s+Space)
for your regex as it also matches multiple spaces and tab characters - if you have multiple rows that you want to affect do gmi
for the flags. I just tried it with the following two strings, each on a separate line in Notepad++
#blablaWith Space
#hello#@#$aWith Space
The replace with value is set to $1WithSpace
and I've tried both replaceAll and replace one by one - seems to result in the following.
#blablaWithSpace
#hello#@#$aWithSpace
Feel free to comment with other strings you want replaced. Also be sure that you have selected the Regular Extension search mode in NPP.
Upvotes: 0