Ziyad
Ziyad

Reputation: 157

Regex for find and replace in N++ of number followed by anything

I have a text file with the following lines:

asm-java-2.0.0-lib
cib-slides-3.1.0
lib-hibernate-common-4.0.0-beta
astp
act4lib-4.0.0

I want to remove everything from, including the '-' before the numbers begin so the results look like:

2.0.0-lib
3.1.0
4.0.0-beta
act4lib

Does anyone know the correct regex for this? So far I have come up with -\D.*(a-z)* but its got too many errors.

Upvotes: 1

Views: 603

Answers (2)

Toto
Toto

Reputation: 91518

  • Ctrl+H
  • Find what: ^.*?(?=\d|$)
  • Replace with: LEAVE EMPTY
  • check Wrap around
  • check Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

^           # beginning of line
  .*?       # 0 or more any character but newline, not greedy
  (?=       # start lookahead, zero-length assertion that makes sure we have after
    \d      # a digit
   |        # OR
    $       # end of line
  )         # end lookahead

Result for given example:

2.0.0-lib  
3.1.0  
4.0.0-beta

Another solution that deals with act4lib-4.0.0:

  • Ctrl+H
  • Find what: ^(?:.*-(?=\d)|\D+)
  • Replace with: LEAVE EMPTY
  • check Wrap around
  • check Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

^           # beginning of line
  (?:       # start non capture group
    .*      # 0 or more any character but newline
    -       # a dash
    (?=\d)  # lookahead, zero-length assertion that makes sure we have a digit after
   |        # OR
    \D+     # 1 or more non digit
  )         # end group

Replacement:

\t          # a tabulation, you may replace with what you want

Given:

asm-java-2.0.0-lib  
cib-slides-3.1.0  
lib-hibernate-common-4.0.0-beta
astp
act4lib-4.0.0

Result for given example:

2.0.0-lib  
3.1.0  
4.0.0-beta
4.0.0

Upvotes: 2

phuclv
phuclv

Reputation: 42002

Use

^\D+\-

If you want to completely remove lines without numbers then use this

^\D+(\-|$)

In case the packages contain numbers in their names like act4lib-4.0.0 then a longer variant is needed

^[\w-]+(\-(?=\d+\.\d+)|$)

It can be shortened to ^.+?(\-(?=\d+\.)|$) but I just want to be sure so I also check the minor version number

The ^ will match from the start of line

Upvotes: 1

Related Questions