Remove Numbers at the Beginning and End of a String

Python: Remove numbers at the beginning of a string

You can remove all digits, dots, dashes and spaces from the start using str.lstrip():

string1.lstrip('0123456789.- ')

The argument to str.strip() is treated as a set, e.g. any character at the start of the string that is a member of that set is removed until the string no longer starts with such characters.

Demo:

>>> samples = """\
... 123.123.This is a string some other numbers
... 1. This is a string some numbers
... 12-3-12.This is a string 123
... 123-12This is a string 1234
... """.splitlines()
>>> for sample in samples:
... print 'From: {!r}\nTo: {!r}\n'.format(
... sample, sample.lstrip('0123456789.- '))
...
From: '123.123.This is a string some other numbers'
To: 'This is a string some other numbers'

From: '1. This is a string some numbers'
To: 'This is a string some numbers'

From: '12-3-12.This is a string 123'
To: 'This is a string 123'

From: '123-12This is a string 1234'
To: 'This is a string 1234'

How to remove numbers from string which starts and ends with numbers?

simple using replaceAll() using ^\\d+|\\d+$ regex that looks for digits in the beginning and ending of the line.

System.out.println("1adfds23dfdsf121".replaceAll("^\\d+|\\d+$", "")); 

output:

adfds23dfdsf

EDIT

Regex explanation:

^     Start of line
\d+ Any digit (one or more times)
| OR
\d+ Any digit (one or more times)
$ End of line

Sample Image

Removing numbers from string

Would this work for your situation?

>>> s = '12abcd405'
>>> result = ''.join([i for i in s if not i.isdigit()])
>>> result
'abcd'

This makes use of a list comprehension, and what is happening here is similar to this structure:

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
if not i.isdigit():
no_digits.append(i)

# Now join all elements of the list with '',
# which puts all of the characters together.
result = ''.join(no_digits)

As @AshwiniChaudhary and @KirkStrauser point out, you actually do not need to use the brackets in the one-liner, making the piece inside the parentheses a generator expression (more efficient than a list comprehension). Even if this doesn't fit the requirements for your assignment, it is something you should read about eventually :) :

>>> s = '12abcd405'
>>> result = ''.join(i for i in s if not i.isdigit())
>>> result
'abcd'

How to remove only numbers from the end of a string?

To fix this use the $ character class to denote that the match should be made at the end of the string. It also makes the g modifier redundant. You can also use + to repeated number and . characters at the end of the string. Try this:

$("button").on("click", function() {  var st = "ABC_1_XY_20".replace(/[\d\.]+$/, '');  console.log(st);});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><button>Click</button>

How to remove digits from the end of a string in Python 3.x?

No, split would not work, because split only can work with a fixed string to split on.

You could use the str.rstrip() method:

import string

cleaned = yourstring.rstrip(string.digits)

This uses the string.digits constant as a convenient definition of what needs to be removed.

or you could use a regular expression to replace digits at the end with an empty string:

import re

cleaned = re.sub(r'\d+$', '', yourstring)

Removing numbers at the end of a string C#

Try this:

string input = "123ABC79";
string pattern = @"\d+$";
string replacement = "";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);

Putting the $ at the end will restrict searches to numeric substrings at the end. Then, since we are calling Regex.Replace, we need to pass in the replacement pattern as the second parameter.

Demo

Remove numbers from end of string if count of numbers(characters) 8

You can use

\d{8,}(?=\.\w+$)
\d{8,}(?=\.[^.]+$)

See the regex demo. If there must be at least 9 digits, replace 8 with 9.

Details:

  • \d{8,} - eight or more digits
  • (?=\.\w+$) - that are immediately followed with a . and one or more word chars and then end of string must follow
  • (?=\.[^.]+$) - the eight or more digits must be immediately followed with a . char and then one or more chars other than a . char till the end of string.

How to remove numbers from a string?

Very close, try:

questionText = questionText.replace(/[0-9]/g, '');

replace doesn't work on the existing string, it returns a new one. If you want to use it, you need to keep it!

Similarly, you can use a new variable:

var withNoDigits = questionText.replace(/[0-9]/g, '');

One last trick to remove whole blocks of digits at once, but that one may go too far:

questionText = questionText.replace(/\d+/g, '');


Related Topics



Leave a reply



Submit