Remove trailing special characters from string

remove special characters from string python
remove special characters from list python using strip
strip python
remove leading and trailing zeros from string in java
remove special characters from end of string python
remove brackets from string python
remove parentheses from string python
scala trim trailing character

I'm trying to use a regex to clean some data before I insert the items into the database. I haven't been able to solve the issue of removing trailing special characters at the end of my strings.

How do I write this regex to only remove trailing special characters?

import re

strings = ['string01_','str_ing02_^','string03_@_', 'string04_1', 'string05_a_']

for item in strings:
  clean_this = (re.sub(r'([_+!@#$?^])', '', item))
  print (clean_this)

outputs this:
string01 # correct
string02 # incorrect because it remove _ in the string
string03 # correct
string041 # incorrect because it remove _ in the string
string05a # incorrect because it remove _ in the string and not just the trailing _

You could repeat the character class 1+ times or else only 1 special character would be replaced. Then assert the end of the string $. Note that you don't need the capturing group around the character class:


For example:

import re
strings = ['string01_','str_ing02_^','string03_@_', 'string04_1', 'string05_a_']

for item in strings:
  clean_this = (re.sub(r'[_+!@#$?^]+$', '', item))
  print (clean_this)

See the Regex demo | Python demo

If you also want to remove whitespace characters at the end you could add \s to the character class:


Regex demo

Python, In this, we employ strip() , which has the ability to remove the trailing and leading special unwanted characters from string list. The map() , is used to extend the  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Remove trailing character(s) from string in Javascript

You could also use the special purpose rstrip method of strings

[s.rstrip('_+!@#$?^') for s in strings]
# ['string01', 'str_ing02', 'string03', 'string04_1', 'string05_a']

Remove Leading and Trailing Characters from a String, In our first solution, we'll create a StringBuilder with the original String, and we'll delete the unnecessary characters from the beginning or the end:. Note, that we use StringBuilder.setLength () instead of StringBuilder.deleteCharAt () when we remove trailing zeroes because it also deletes the last few characters and it's more performant.

You need an end-of-word anchor $

 clean_this = (re.sub(r'[_+!@#$?^]+$', '', item))


Remove Trailing Special Characters - Support, How can I remove/delete trailing special characters? or a regexp function, which removes all non-word characters at end of the string . In this case, you can simply add the space character as one of the characters to be trimmed: SELECT TRIM('= ' FROM ' =SPECIALS= ') AS Result; Result: Result ----- SPECIALS. For more examples of using TRIM() to trim whitespace from the left and right of a string, see How to Remove Leading and Trailing Whitespace in SQL Server.

Python String strip() method, The strip() method returns a copy of the string by removing both the leading and the trailing characters (based on the string argument passed). The TRIM function allows you to trim leading and/or trailing characters from a string. The following shows the syntax of the TRIM function. TRIM([LEADING | TRAILING | BOTH] trim_character FROM source_string); First, specify the trim_character, which is the character that the TRIM function will remove.

I want to remove the special characters as well as extra spaces , Trim method will remove only trailing/leading spaces but not spaces in between string, right? Cheers. Vivek_Arunagiri March 27, 2019, 2  When you know a specific character to remove from the cell value, just use that character as remove_char in the formula Use the formula = SUBSTITUTE (A3,"!","")

Remove Leading and Trailing Characters, Python strip & Rust trim¶. Spaces¶. Result : “sqeeze me”. # Python # Setup s =  The inner translate () removes all characters of interest (e.g., numbers) to obtain a from string for the outer translate (), which removes these non-numeric characters from the original string. Sometimes you do not want to remove all occurrences of whitespace, but instead want to remove leading, trailing, and redundant internal whitespace.