1

I need a regular expression to use in python that captures one group containing the whole string minus the last 2 characters, but if the string have a "-" , then ignore it and all after it.

Ex:

abcde = abc

jklmno-pqrs = jklm

I think it would be a mix between (.*)..$ and ^([^-]*) , but I dont know how to combine them.

  • Maybe this helps: https://stackoverflow.com/questions/869809/combine-regexp – zabop Sep 30 '21 at 18:32
  • Why don't you just use a regex to extract the string until `-` and then use `[:-2]` to remove the last 2 characters? – mkrieger1 Sep 30 '21 at 18:39
  • If you write this as a running program that demonstrates the problem, including a print of what you get from your regex, then we have the basis for tested solutions. – tdelaney Sep 30 '21 at 18:45

2 Answers2

3

You could use a capture group matching any char except - followed by matching 2 chars other than -

^([^-\n]+)[^-\n]{2}(?:-.*)?$

In parts, the pattern matches:

  • ^ Start of string
  • ([^-\n]+) Capture group 1, match 1 or more chars other than - (add \n to not match a newline)
  • [^-\n]{2} Match 2 chars other than -
  • (?:-.*)? Optionally match - followed by 0+ times any char
  • $ End of string

Regex demo

For example

import re
 
pattern = r"^([^-\n]+)[^-\n]{2}(?:-.*)?$"
s = ("abcde\n""jklmno-pqrs")
 
print(re.findall(pattern, s, re.M))

Output

['abc', 'jklm']
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • @JefersonCorrea You are welcome. If you don't need the whole match, you might also shorten the pattern to `^([^-\n]+)[^-\n]{2}` See https://regex101.com/r/tqlrGn/1 – The fourth bird Sep 30 '21 at 19:15
-1

You can combine two regex using pipe delimiter like this

re.findall('(.*)..$|^([^-]*)', string)

for reference '|' pipe delimiter is used for grouping multiple regular expressions

Assad Ali
  • 288
  • 1
  • 12