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$+;
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$+;
  • working prefect as always you amazing me @Toto Thanks so Much For Your Supportmanlike – DeathRival Oct 16 '17 at 2:09
  • @DeathRival: You're welcome, glad it helps. – Toto Oct 16 '17 at 9:41

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.