0

let's say I have text documents that look like that

1   
M1577682
Wayne   United States   Minnesota   Minneapolis 55  2019-10-31 14:51:05
2   
M1527197
henrik  Denmark Sjelland    Koge    52  2019-10-31 14:29:53
3   
M3455913
Kim Canada  Ontario London  61  2019-10-30 21:36:03
4   
M2040689
shapo   Germany Hesse-Darmstadt Frankfurt   45  2019-10-31 13:19:12

I need to make a line from that text that looks like this.

M1577682,M1527197,M3455913,M2040689

I have found that this ^(?!.M1577682)(.?)$ will select anything that does not contain M1577682.

And this \bM\w{7,} will select any words that start with M and have 7 or more letters after that. (still selects Minnesota and Minneapolis too, I can't fix that)

So can I somehow merge those two regex thingies together to select everything that doesn't start with M and has 7 digits(letters) and replace it with a comma?

1

Here is a way to go, after that you have to remove manualy the last comma.

  • Ctrl+H
  • Find what: (?:(?:(?!M\d{7}).)*|\G)(M\d{7})(?:(?!M\d{7}).)*
  • Replace with: $1,
  • CHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • CHECK . matches newline
  • Replace all

Explanation:

(?:                 # non capture group
    (?:             # non capture group
        (?!         # negative lookahead, make sure we haven't after:
            M       # the letter M
            \d{7}   # 7 digits
        )           # end lookahead
        .           # any character
    )*              # end group, may appaear 0 or more times
  |                 # OR
    \G              # restart from last match position
)                   # end group
(                   # group 1
    M               # the letter M
    \d{7}           # 7 digits
)                   # end group 1
(?:                 # non capture group
    (?!             # negative lookahead, make sure we haven't after:
        M           # the letter M
        \d{7}       # 7 digits
    )               # end lookahead
    .               # any character
)*                  # end group, may appear 0 or more times

Note: You may be want to add word boundaries, if it's the case replace M\d{7} with \bM\d{7}\b everywhere in the regex.

Screen capture (before):

enter image description here

Screen capture (after):

enter image description here

| improve this answer | |
  • Oh this is wonderful, works perfectly for me. Thank you a lot! – Borschik Oct 31 '19 at 17:48
  • @Borschik: You're welcome, glad it helps. – Toto Oct 31 '19 at 17:55
0

If the format is "M" followed by 7 sequential digits, we can try to match what we want to keep and negate it using the following regex:

^(?!M[0-9]{7}).*

where the inner portion is

  • M - Matches "M" exactly
  • [0-9] - Matches a single character in the list containing the digits zero to nine.
  • {7}- A quantifier for the above list (match 7 digits)

and the outer portion is the negative lookahead. Try it here!

| improve this answer | |
  • And how do you get the expected result? – Toto Oct 31 '19 at 17:31
  • Thank you, it helped me understand how do the regexes behave. I can make it work in conjunction with a second step. – Borschik Oct 31 '19 at 17:45

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.