-4

I have a string that will have a value somewhere along the lines of

#549382This/ *is a test&

And I want to remove the #549382 from the text.

I know there are a lot of questions about regex and I have looked at this one specifically which would work if I knew the character to remove. But any letter or symbol can follow that string of numbers. I need a way to be able to say

Give me all characters between the '#' and the first letter

What the letter is does not matter but it can be any non-digit character or letter.


For example

#549382This is a test         ->    This is a test
#71290571Another test here    ->    Another test here
#276//a comment as well       ->    //a comment as well
Luke
  • 138
  • 2
  • 11
  • 1
    by "any alphabetical number" you mean "any non-digit character," yes? – Esther Jun 15 '22 at 15:01
  • Yes I do. I will edit the post to include that – Luke Jun 15 '22 at 15:02
  • regex is your friend – SuperStew Jun 15 '22 at 15:02
  • 1
    Please clarify whether you want to remove everything before the text, as your examples imply, or get the number between the # and the text, as your title and description imply. – Krateng Jun 15 '22 at 15:03
  • I'm confused. You say you want "all the characters **between** the # and the first non-digit character" but your examples do not reflect this. – ddejohn Jun 15 '22 at 15:03
  • Either one works, if I get them then I can match and remove them using .replace. If I can remove them using regex that also works for me. – Luke Jun 15 '22 at 15:05

3 Answers3

1

Try something like this. can put it in a loop or whatever

import re
teststr='#549382This is a test'

e='#[0-9]*(.*)'

re.findall(e,teststr)
SuperStew
  • 2,857
  • 2
  • 15
  • 27
1

As for your question, 'Give me all characters between the '#' and the first letter what the letter is does not matter but it can be any alphabetical number, meaning any non-digit character.', the following code will do:

import re

cases = [
    "#549382This is a test",
    "#71290571Another test",
    "#276//a comment as well",
]
regex_pattern = '#(\d+)'
for case in cases:
    number = re.findall(regex_pattern, case)
    print(number)

>>> ['549382']
>>> ['71290571']
>>> ['276']

Explaination: The regex will all digits (\d+) after the # and up to any non-digit character.

1

you can use regex like this

import re
string = '#549382This is a test'
result = re.sub('^#\d*', '', string)
This is a test