Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex excluding
#1
How could I use regex to match everything not in between hyphens? E.g. re.search(r"<>", "- i don't want this - I do want this")

Edit: This particular example is simple, and could be solved using re.split() -- but that won't generally work
Reply
#2
Replace the bits you don't want with nothing.

target = "Stuff at front - i don't want this - I do want this"
wanted = re.sub(r'-.*-', '', target)
print(wanted)
Output:
Stuff at front I do want this
Reply
#3
You can do it without regex with str methods.

def get_this(text):
    left, _, right = text.rpartition("-")
    return right.strip()


data = "- i don't want this - I do want this"
result = get_this(data)
print(result)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help excluding Named Entity (NE) and proper nouns (NNE) from text analysis disruptfwd8 0 2,341 May-15-2018, 12:10 AM
Last Post: disruptfwd8

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020