Shifting a block of text to the beginning of a line
-
It is going to depend upon how strongly formatted your data is…but if it is as your example (which really isn’t very rigorous) shows, then this should work:
Find what zone:
(?-s)^(.+?), (#.+?),
Replace with zone:\2, \1,
The explanation is really too simple to turn it over to RegexBuddy:
(?-s)
-> any following.
characters may NOT match across line-boundaries
^
-> match at the beginning of a line only
(.+?)
-> match one or more characters, but as few as possible, capture data into group #1
,
-> match a comma and a space, in that order
(#.+?)
-> match a octothorp plus one or more characters, but as few as possible, capture data into group #2
,
-> match a commaAt replace time, we remove the match above, and substitute:
\2
-> the contents captured above as group #2
,
-> a comma and a space
\1
-> the contents captured above as group #1
,
-> a commaAs you can see, this effectively swaps the first two comma separated fields on a line, but only if the second field begins with
#
-
Dear Mr Scot Sumner
Thank you so much! I was getting some “guru meditation” error while trying to open the notepad++ community site. But today I did not.I did exactly as you said. Actually to avoid typos, I copied and pasted the expressions into the search and replace fields.
But notepad++ says :0 occurrences were replaced.
Thank you for your help!
-
So all we really have to go on is the data you provided…it looks like this:
#12-3-23/B, Some Street, Some village, Some District C/o Some Name, #9-22-1, Some Village, Some district
If I run my suggested replacement on that, I get this:
#12-3-23/B, Some Street, Some village, Some District #9-22-1, C/o Some Name, Some Village, Some district
…and this seems to be what is wanted. I don’t really have any idea what you are doing differently. I’d suggest that you don’t have the Search mode set correctly, but you mentioned “regex” so I presume that you know you need to set the mode to Regular expression.
-
Dear Mr Sumner
The data I provided is absolutely in the same format as two real addresses I have in my text file. Except that I removed names of persons and villages for the purposes of privacy and gave random digits and alphabets for addresses. The two lines are exact in so far as the spaces, commas and hash symbols are concerned. Some lines have more comma separated text, but most of the addresses have the door numbers in the second position (after the first comma - if you know what I mean). I removed training and leading spaces.I have the ‘Regular Expression’ radio button pressed in the ‘Search Mode’ box in notepad++.
Is there a way I can send you my screen shot?
Or should I remove all white spaces and then try again?
Best regards!
-
Are you absolutely certain there is only 1 space after the comma? From the regex that was supplied its most likely the
, #
sequence where the issue lies. Unless you counted the white spaces by moving through it with the cursor you won’t know for sure.As a test do a Find, not Replace and type in the sequence I typed above in red. If that doesn’t catch any occurrences try it with 2 spaces, see if that finds any.
Terry
-
Dear Mr Terry R
I did the following to the text file after first opening it:- Remove leading and trailing spaces
- Replace all occurrences of 2 consecutive white spaces with 1 space. (There were even 3 spaces, which I removed and now there is just a single white space after every comma in every line)
- Remove commas that were at the end of some lines.
- I added a hash symbol to all the lines that began with a digit using the regex Mr Scott Sumner suggested. It worked.
so far so good.
but when I tried the regex for swapping the first and second blocks, it did not work.
Best regards and thanks!
-
I think we are at the limits of how we can provide help, unless you are willing to show us some data where you say it doesn’t work. This doesn’t have to be EXACT data which would be a violation of privacy–to prevent that use some kind of substitution cipher on it. Just make sure you only substitute letters for letters and numbers for numbers, and keep all other characters the same, including spacing.
People seem to have trouble showing exact data here, so what I’ll say to do is use Notepad++ to get the data exactly how you want to present it, and then copy it into this website…but right before the copy from Notepad++, add FOUR spaces to the start of each line. If you do that, when you preview your posting on this site, it will have a black background, like this:
hello there I am properly formatted EXACT data that cannot possibly be misunderstood
-
Dear Mr Sumner
I am posting 6 lines of addresses and following your advice of adding 4 spaces before each line. I took precautions for privacy by giving fictitious names and door numbers. But the format is exactly the same asH/O Kamala Devi, #34-9-321/D, Siva Lakshmi Nagar, Turupuralla (U), Mandya Dist, 22334455666 #25/432-A, Kondapalli (V), Galimeda (M), Kolar Dist #4-3-429, Koundinyapuram, Bhongir (U), Nellore Dist H/O Meenakshi Shetty, #123-11/3, N G Layout, Tanuku (U), Buvanagiri Dist H/O Santha Kumari, Ramannapet (V&P), Darsi (M), Chittoor Dist, 0123-4567890 S/O Gopal Reddy, #56-357, Martoor (V), Mulabagal Dist (M&D)
Thanks and best regards!
-
So if I use the regular expressions given before on that data, I get as a result:
#34-9-321/D, H/O Kamala Devi, Siva Lakshmi Nagar, Turupuralla (U), Mandya Dist, 22334455666 #25/432-A, Kondapalli (V), Galimeda (M), Kolar Dist #4-3-429, Koundinyapuram, Bhongir (U), Nellore Dist #123-11/3, H/O Meenakshi Shetty, N G Layout, Tanuku (U), Buvanagiri Dist H/O Santha Kumari, Ramannapet (V&P), Darsi (M), Chittoor Dist, 0123-4567890 #56-357, S/O Gopal Reddy, Martoor (V), Mulabagal Dist (M&D)
So it seems to be working; here’s what the setup looks like for it:
Here’s a thought: What version of Notepad++ are you using?
-
Dear Mr Sumner
Thank you! It worked now.
I am a little embarrassed to say, but I do not know what I did wrong the first time and what I did right this time.Best Regards!
PS. I am using Notepad++ v7.5.8 32bit