0

Hi I would like to get everything after the '_' using regex

For example: I have --> I want

'aaa_bbb_ccc' --> 'bbb_ccc'
'dd_aaaa_1' --> 'aaaa_1'
'*/_2d*_//' --> '2d*_//'

Is there anyway to do it?

Thanks in advance.

FAD
  • 21
  • 2

1 Answers1

1

I rather like the split suggestion given by @Maroun in a comment above. Here is an option using re.sub:

x = "aaa_bbb_ccc"
output = re.sub(r'^[^_]+_', '', x)
print(output)

bbb_ccc

The regex does not require much explanation, and it just removes all content up to, and including, the first underscore in the input string.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks. Tbh I asked because I am trying to get better with regex but in fact i m struggling with this. – FAD Dec 11 '18 at 13:35
  • 1
    @FAD Next time you ask, make sure to include your current Python script. Had you done this, you probably would not have been downvoted. Nothing at all wrong with asking for help it you need it :-) – Tim Biegeleisen Dec 11 '18 at 13:36