xxxxxxxxxx
import re
# Regular expression to match only numbers
regex = r'^\d+$'
# Test cases
tests = ['123', '56789', '0', '12a34', 'abc']
for test in tests:
if re.match(regex, test):
print(f"{test} is a number")
else:
print(f"{test} is not a number")
xxxxxxxxxx
export const onlyNumbersAndLetters = (value) => {
if(value)
return value.replace(/[^a-zA-Z0-9]/g, '');
}
xxxxxxxxxx
console.log(`12331: ${/^\d+$/.test('12331')}`) // true
console.log(`abc: ${/^\d+$/.test('abc')}`) // false
xxxxxxxxxx
import re
# Example string
text = "The price of the product is $99.99."
# Regular expression pattern to match only numbers
pattern = r'\d+'
# Find all matches in the text
matches = re.findall(pattern, text)
# Print the extracted numbers
print(matches)
xxxxxxxxxx
import re
def keep_only_numbers(text):
# Apply regular expression to keep only numbers
numbers_only = re.sub(r'\D', '', text)
return numbers_only
# Example Usage:
input_string = "abc123!@$%^456def"
result = keep_only_numbers(input_string)
print(result) # Output: 123456
xxxxxxxxxx
import re
text = "abc123xyz456"
numbers_only = re.findall(r'\d+', text)
print(numbers_only)