0

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

3 Answers3

1

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.

(#+.*?)With Space

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.

#+(?:.*?)\KWith 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.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Thank you but unfortunately with ^ in Notepad++ this is not working :( – Kenan Trgic Jul 16 '18 at 19:36
  • I have added another option to my answer. Can you try the first part? Did you enable Regular expression in Notepad? – The fourth bird Jul 16 '18 at 19:36
  • I don't know why but the search is only working without the ^ expression – Kenan Trgic Jul 16 '18 at 19:40
  • @KenanTrgic `^` asserts the start of the line. Are the hashtags at the start or could they also be in the middle of a line? In that case you can omit the `^` and use [`(#+.*?)With Space`](https://regex101.com/r/tTDAPc/2) and replace with `$1WithSpace` – The fourth bird Jul 16 '18 at 19:42
  • Yes it can be also in the middle, I tried this one but it marks to much I think see the screenshot here [ScreenShot](https://i.imgur.com/855vlUn.png) – Kenan Trgic Jul 16 '18 at 19:49
  • It matches to text in you example [`(#+.*?)+Aachen Brand`](https://regex101.com/r/GwMU3d/1). Then replace with `$1AachenBrand` – The fourth bird Jul 16 '18 at 19:52
  • now the problem is that there is also HTML text which in this case is highlighted see screenshot [screenshot](https://i.imgur.com/Qt4khkR.png) – Kenan Trgic Jul 16 '18 at 19:56
  • Ah I see, it is [not advisable to use regex for html](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). Can the tags contain a double quote? You might try this if they don't [`(#+[^"\s]+)+Aachen Brand`](https://regex101.com/r/ag1D7l/1) – The fourth bird Jul 16 '18 at 20:09
0

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.

Danail Gabenski
  • 2,870
  • 1
  • 21
  • 27
0

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"));
Nick Abbott
  • 364
  • 1
  • 9