Home > other >  Regex to negate certain characterss
Regex to negate certain characterss

Time:09-29

I need some help with regex to extract specific characters from a given string. Following is the string and required string to extract.

21.4R3.13-EVO -- Required 21.4R3-EVO 
24.1R13.13-EVO -- Required 24.1R13-EVO 
21.4R2-S1-20190223000107.0-EVO  -- Required 21.4R2-EVO 
19.4R11-S1-20190223000107.0-EVO  -- Required 19.4R11-EVO 

I tried following and its matching first half but I couldn't negate the middle section.

[0-9] .[0-9] [A-Z][0-9] 

Matching
21.4R3

CodePudding user response:

You can use this regex for search:

^(\d \.[a-zA-Z\d] )[-.].*(-[a-zA-Z] )$

and replace with $1$2.

RegEx Demo

RegEx Breakup:

  • ^: Start
  • (\d \.[a-zA-Z\d] ): Match 1 digits then a dot followed by 1 alpha numeric character. Capture this in group #1
  • [-.].*:
  • (-[a-zA-Z] ): Match last - followed by 1 letters and capture in group #2
  • $: End
  • Related