0

How do I remove anything not letters or numbers from part of lines in txt file?

More explaining I have username:password or username;password.

I want to edit the username only and remove anything that not letters or numbers from it without touch the password. I would like do it with Regular Expressions since I am using Emeditor For Large Files and I believe Regular Expressions work in both Notepad++ and Emeditor.

What have I tried?
I did use Find: [^a-z0-9:;]+ but cant skip the password after : or ; so what I want to do is to skip line start from :password here and ;password here and only remove none letters or numbers from username alone.

Sorry if I didn't describe everything well, admins can edit if possible.

Full Example of lines:

!start._1:stop.~1@
Sta%rs&:B!ge(s+R}\
#Step[14,:St,./\Ert`
~user@#%name^*)+:P@$$wor'";D
T&*est~!@#$%^&*()_+={}|\;pass;word123
user@#%name;password!#$~`'123
45Star^5#$Lord1:@T1esting!
u~s#e%r^n&a*m(e)t_e+s-t,:Pa:ssw/orD$+;

Needed Result:

start1:stop.~1@
Stars:B!ge(s+R}\
Step:St,./\Ert`
username:P@$$wor'";D
Test;pass;word123
username;password!#$~`'123
45Star5Lord1:@T1esting!
usernametest:Pa:ssw/orD$+;
8
  • We are not here to teach you how to use regular expressions. Can you please at least make an effort to find your own solution first? You have asked many many questions about regexp and you don't seem to learning from the answers given to you.
    – DavidPostill
    Commented Oct 15, 2017 at 13:04
  • i've learned a lot from the answers that given and i ask this question because i tried a lot but i didn't figure out how to skip anything after the : and ; which will be the password and thanks for your kindless anyway
    – user677589
    Commented Oct 15, 2017 at 13:07
  • So tell us what you tried and why it didn't work. From superuser.com/help/how-to-ask: "Have you thoroughly searched for an answer before asking your question? Sharing your research helps everyone. Tell us what you found and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and above all, it helps you get a more specific and relevant answer!"
    – DavidPostill
    Commented Oct 15, 2017 at 13:12
  • alright i tried Find: [^a-z0-9:;] the problem this don't skip password so if there anything not letters or numbers in the password will be removed too and i don't want this happen i want skip the line from : and ; included the password and only the work be in the username but i don't know what to do in [^a-z0-9:;] to avoid the password
    – user677589
    Commented Oct 15, 2017 at 13:28

1 Answer 1

1

It can't be done in one pass.
Here is a way to do the job with multiple passes:

  • Ctrl+H
  • Find what: ^([^a-z0-9;:]*)([a-z0-9]*)(?1)(.*?[;:].+$)
  • Replace with: $2$3
  • Uncheck Match case
  • check Wrap around
  • check Regular expression
  • DO NOT CHECK . matches newline
  • Replace all (Click here as many times as needed, it replaces only a part of invalid characters each time)

Explanation:

^                   : begining of line
  (                 : start group 1
    [^a-z0-9;:]*    : negative character class, 0 or more any character that is not alpha-num or colon or semicolon
  )                 : end group 1
  (                 : start group 2
    [a-z0-9]*       : character class, 0 or more alpha-num
  )                 : end group 2
  (?1)              : re-use the pattern in group 1 (ie. [^a-z0-9;:]*)
  (                 : group 3
    .*?             : 0 or more any character but newline, not greedy
    [;:]            : a colon or semicolon (the first that exists in a line)
    .+              : 1 or more any character but newline (the password)
    $               : end of line
  )                 : end group 3

Replacement:

$2      : content of group 2, the alpha-num part of the name
$3      : content of group 3, rest of the line

Result for given example:

start1:stop.~1@
Stars:B!ge(s+R}\
Step14:St,./\Ert`               <== I guess there is a typo in your request
username:P@$$wor'";D
Test;pass;word123
username;password!#$~`'123
45Star5Lord1:@T1esting!
usernametest:Pa:ssw/orD$+;
2
  • working prefect as always you amazing me @Toto Thanks so Much For Your Supportmanlike
    – user677589
    Commented Oct 16, 2017 at 2:09
  • @DeathRival: You're welcome, glad it helps.
    – Toto
    Commented Oct 16, 2017 at 9:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .