0

I have the following text in my NotePad++ file

H3J 2J6,H3J 2J7,H3J 2Z8,H3J 2T8

(there is actually 100s of lines in this format), how do I remove the first 4 characters (including the space) so it's displayed as

'H3J', 'H3J', 'H3J', 'H3J',

The idea is to parse the text and have it ready to INSERT INTO a database afterwards.

Thank you.

1

I'm sure you already know, but for a full answer, you can do this using regex and the find and replace dialog.

Find: .{3}\s(.{3})

Replace With: \'$1\'

Search Mode: Regular Expression

.{3}\s Matches 3 chars then a space (.{3}) matches the next three chars and captures as a group.

\'$1\' Replaces the text that was found in the above statement with the group we captured adding quotes around it. Leaving any commas as they originally were.

With regex there are many ways to do this, this is just one example.

Regex

Edit:

From your comment I believe you wanted the the first item from the first group, and the second item from any following three groups.

The regex for that would be:

Find: (.{3})\s.{3},.{3}\s(.{3}),.{3}\s(.{3}),.{3}\s(.{3})

Replace With: \'$1\',\'$2\',\'$3\',\'$4\',

If you want to get the first group like your example now reads. Use the following regex:

Find: (.{3})\s(.{3})

Replace: \'$1\'

and finally to add commas to the end of every line, use this:

Find: $

Replace: ,

  • Great answer mt025, thank you for the detailed response. The regex seems to discard the first 3 characters and keep the last 3 characters, is there a way to keep the first 3 characters instead? – user3591436 Aug 31 '16 at 17:39
  • I'm a little confused with the question? So you want the the first item from the first group, and the second item from any following groups? – mt025 Aug 31 '16 at 17:44
  • I have added that to the answer anyway. Let me know if that's what you wanted. – mt025 Aug 31 '16 at 17:49
  • mt025 you're right, I apologise as my original question was off. I have edited the question now, I actually need the first 3 characters of each group. I'm going to mark your answer anyways since it answers the original question. – user3591436 Aug 31 '16 at 17:59
  • Ah okay, I ill change my original answer to reflect that. – mt025 Aug 31 '16 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.