Regex to get all items between '#' and unknown character
Question:
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
Answers:
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)
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.
you can use regex like this
import re
string = '#549382This is a test'
result = re.sub('^#d*', '', string)
This is a test
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
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)
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.
you can use regex like this
import re
string = '#549382This is a test'
result = re.sub('^#d*', '', string)
This is a test