Godicex
Godicex

Reputation: 23

How to remove the hash tag from this regex

My current regex:

(\d.{17})[^#]*(\D+)(\d+)gr(\d+)

In group 2, they are still having the hashtag, I want to remove it from there. What should I change from my current regex?

201223E0MWJPJD2230#AdeSaputra290gr99000
2101023CNV6TT1109J#Fefe430gr142000
2101183EDTFPSA0128#Jessica500gr112000
201221E2QKWRY11413#EssyYosita880gr233500
2101123G9XQ7R41705#Meily1120gr329000
201228ECEWTJT50859#WidyaNatali1720gr457230
201227EEBX1K9K1020#Excelio112gr58900
2101112N4YNFB12016#DebyNath520gr156220
2101072R8A0QB22347#AlycieHandoTan700gr85000

Output:

group 1: 201223E0MWJPJD2230
group 2: #AdeSaputra
group 3: 290
group 4: 99000

Upvotes: 2

Views: 310

Answers (2)

Ryszard Czech
Ryszard Czech

Reputation: 18631

\D matches any non-digits and it matches # in your input.

Add # to the pattern instead of the opposite [^#] so as to match it.

Use

^(\d.{17})#(\D+)(\d+)gr(\d+)$

See proof it works. Adding anchors to match entire strings.

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
    .{17}                    any character except \n (17 times)
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  #                        '#'
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    \D+                      non-digits (all but 0-9) (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \3
--------------------------------------------------------------------------------
  gr                       'gr'
--------------------------------------------------------------------------------
  (                        group and capture to \4:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \4
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

Upvotes: 0

Ali Faris
Ali Faris

Reputation: 18610

remove [^] from your regex

(\d.{17})#*(\D+)(\d+)gr(\d+)

see this

Upvotes: 1

Related Questions