How will I achieve below mentioned output.
I wanted to remove multiple occurrences of the **ONLY **special character **not words or digit. **
Link to Original Image of Data
Data | Output Needed |
---|---|
CL-LIN-VM--OE382F | CL-LIN-VM-OE382F |
CYaIX-PHY--aG617A | CYalX-PHY-aG617A |
FRWIN-VM--FO025B | FRWIN-VM-FO025B |
PYWIN-VM--IK043D | PYWIN-VM-IK043D |
MQAIX--PhQA622F | MQAIX-PhQA622F |
I tried this code but its replace all the value
Single_Column['Workload_2'].replace(regex=True,
inplace=True,
to_replace=r"[^a-zA-Z0-9 -]",
value=r'-')
CodePudding user response:
This "[^a-zA-Z0-9 -]
" Pattern will select all values individually that are not present in given Range pattern, or equal to characters " " (Space) or "-" (Hyphen), because of this "--" value will never be Replaced since "-" (Hyphe) is present in pattern.
If you only remove "-" (Hyphen) from pattern, "--" (Double Hyphen) this will be considered as two separate "-" (Hyphen) and replace function will replace each "-" (Hyphen) once so you will again end up with Double "-" (Hyphen) i.e. Same Number of Hyphen that were originally present.
So add " " Quantier at end of Set ([]). this will match One or more occurrences of pattern (token) found earlier in this case Pattern found using Set ([]) i.e. all Characters not present in Set ([]), and will give it to you as one single String or Match.
Heance Use This Pattern: [^a-zA-Z0-9 ]
"^" will give characters that are not present in given Range pattern and/or equal to character " " (Space)
" " matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
" " will create a string of special characters irrespective of how many times it appears, so we just have to replace entire string at once with value that we want
PS:
You Can Use Below or Other Similar Site for Checking and Debugging Your Regex Patterns, on right side of site you can see Explanation and Matches.
Check that to understand how your pattern will work and give you results
Website Link: regex101: build, test, and debug regex
Regular Expression: /[^a-zA-Z0-9 ] /gm
Test String:
sadada$^%$^&$&--RE#$##%#rtewe--- #$%1soro#%$124093@#%$!0=?><:[3433165^$%#@5
Result (All Matches):
$^%$^&$&--
#$#
#%#
--- #$%
#%$ @#%$!
=?><:[ ^$%#@