Regular Expression - find and replace substring - further replacing all spaces in only defined substring with underscores
-
Hi Notepad++ Users,
Could you please help me with the following search-and-replace problem I am having?
I need to select a substring withing a string and replace all spaces with underscores
Here is a sample of the data I currently have (“before” data):
0000000001 001 0001 00000001 1 00000000 0000000000 Text with spaces 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01 0000000001 001 0001 00000001 1 00000000 0000000000 Long variable length text with spaces 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01 0000000001 001 0001 00000001 1 00000000 0000000000 A B C 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01
Here is how I would like that data to look (“after” data):
0000000001 001 0001 00000001 1 00000000 0000000000 Text_with_spaces 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01 0000000001 001 0001 00000001 1 00000000 0000000000 Long_variable_length_text_with_spaces 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01 0000000001 001 0001 00000001 1 00000000 0000000000 A_B_C 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01
NOTE:
-the first part of each string ‘\1’ is fixed length <— (^13[ 0-9]{51}) finds the first part
-the second part of the strings ‘\2’ are the parts that have whatever characters with spaces where spaces need to be replaced with “_” <— (*) finds this second part
-the third part of the strings ‘\3’ have variable lengths and alphanumeric characters <— ( [0-9]{3} [0-9]{2,22} [DC] *^13) seems to do the trickTo accomplish this, I have tried using the following Find/Replace expressions and settings
- Find What =
(^13[ 0-9]{51})(*)( [0-9]{3} [0-9]{2,22} [DC] *^13)
<=== Using MS Word(###but I would like to use NotePad++###) - Replace With =
^13\2^13
<=== Using MS Word(###but I would like to use NotePad++###) - Search Mode = wildcards <=== Using MS Word(###but I would like to use NotePad++###)
HERE IS WHY YOU THOUGHT YOUR EXPRESSION WOULD WORK
This returns just the substring of interest (the text where spaces need to be replaced with underscores) - \1 and \3 are omitted in replace with
Unfortunately, this does not produce the output I desired
I need to further process part of the found string - ‘\2’ - replacing " " with “_”
To get to what I need, I’d need to somehow use Replace with = Replace With =^13\1{replace(\2," ","_")}\3^13
<=== curly brackets {} enclosing the needed replacement of substring ?Could you please help me understand what went wrong and help me find the solution?
- Find What =
-
If I were doing it, I’d try applying this technique: https://community.notepad-plus-plus.org/topic/22690
-
Hello, @j-s-1,
If you’re having trouble creating the right regular search expression, just ask me about it ;-)
Best Regards,
guy038