How to remove commas and dots of individual word in two dimensional list?
Try with my solution:
list = [['Hello.', 'My', 'World,']]
list_n = []
for l in list:
n = []
for e in l:
e = e.replace('.', '')
e = e.replace(',', '')
n.append(e)
list_n.append(n)
print(list_n)
Output:
[['Hello', 'My', 'World']]
PHP remove commas from numeric strings
Not tested, but probably something like if(preg_match("/^[0-9,]+$/", $a)) $a = str_replace(...)
Remove comma only in specific locations
For a pure regex solution, you can use
""|,(?=(?:(?!"").)*?"",)
and replace with the empty string.
https://regex101.com/r/LVs4sT/1
It matches either ""
, or a comma which is eventually followed by "",
, which ensures that the comma is inside a ""<data>""
section.
str = '01-01-2010,a,""0.0"",c,d,""1,234,567.00"",1,2,3,4'
re.sub(r'""|,(?=(?:(?!"").)*?"",)', '', str)
If the ""
s can occur at the end of the string as well, then rather than matching just the comma at the end of the lookahead, use (?=,|$)
.
How can I split commas and periods from words inside of string using split?
I think your real question is "How do I replace a substring with another string?"
Checkout the replace method:
let inputString = "Hi, my name is John.";
let switch1 = ["John", "Jack"];
let switched = inputString.replace(switch1[0], switch1[1]);
console.log(switched); // Hi, my name is Jack.
UPDATE: If you want to get ALL occurrences (g
), be case insensitive (i
), and use boundaries so that it isn't a word within another word (\\b
), you can use RegExp:
let inputString = "I'm John, or johnny, but I prefer john.";
let switch1 = ["John", "Jack"];
let re = new RegExp(`\\b${switch1[0]}\\b`, 'gi');
console.log(inputString.replace(re, switch1[1])); // I'm Jack, or johnny, but I prefer Jack.
Javascript - Removing commas from an array of string numbers
You can use number in same map
callback function, g
to remove all occurrence of ,
dataArr = [" 1,431,417 ", " 1,838,127 ", " 679,974 ", " 2,720,560 ", " 544,368 ", " 1,540,370 "]
let arrData = dataArr.map(e => Number(e.replace(/(,\s*)+/g, '').trim()));console.log(arrData)
regex to remove multiple comma and spaces from string in javascript
You can just replace every space and comma with space then trim those trailing spaces:
var str=" , this, is a ,,, test string , , to find regex,,in js. , ";
res = str.replace(/[, ]+/g, " ").trim();
jsfiddle demo
How to remove commas from numeric values in a string with other commas?
You could also use array_map with preg_replace_callback and as the pattern you could use:
"\d{1,3}(?:,\d{3})+\.\d{2}"
"\d{1,3}
Match"
followed by 1-3 digits(?:,\d{3})!
Repeat 1+ times matching a comma and 3 digits\.\d{2}"
Match a dot and 2 digits followed by"
Regex demo | Php demo
In the callback of preg_replace_callback
replace the comma with an empty string and return the match.
For example:
$atm = array(
'26' => '20/08/2099,"ATM CASH WITHDRAWAL (ON-US) ATM CASH WITHDRAWAL (ON-US) EMPEMOM, LAGA ATM 2 LOGO NG 000360585490","",20/08/2018,"5,000","","1,316.01"',
'27' => '27/08/2027,BANK CHARGE 26 SMS CHARGE AND VAT FOR 27TH JUL - 23RD AUG 2018,2803064 028,27/08/2018,109.2,"","1,206.81"'
);
$atm = array_map(function($x) {
return preg_replace_callback('/"\d{1,3}(?:,\d{3})+\.\d{2}"/', function($m) {
return str_replace(',', '', $m[0]);
}, $x);
}, $atm);
print_r($atm);
Result:
Array
(
[26] => 20/08/2099,"ATM CASH WITHDRAWAL (ON-US) ATM CASH WITHDRAWAL (ON-US) EMPEMOM, LAGA ATM 2 LOGO NG 000360585490","",20/08/2018,"5,000","","1316.01"
[27] => 27/08/2027,BANK CHARGE 26 SMS CHARGE AND VAT FOR 27TH JUL - 23RD AUG 2018,2803064 028,27/08/2018,109.2,"","1206.81"
)
Replace periods and commas with space in each file within the folder then print files
your problem is not replace
but for word in text
which split into chars, not words.
This code
stopwords = ['and', 'or']
text = "Hello World. Bye."
tokens_without_sw = [word for word in text if word not in stopwords]
print(tokens_without_sw)
gives
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '.', ' ', 'B', 'y', 'e', '.']
but you expected ["Hello", "World.", "Bye."]
You shoudl use text.split(" ")
to get words - but first you should replace commas
and dots
because you may have text without spaces like "World.Bye"
and then it will treat it as single word. Besides if you don't replace commas
and dots
before removing stop_words_list
then it may keep stopwords with dots
or commas
- like or...
stopwords = ['and', 'or']
text = "Hello World. Bye."
text = text.replace('.', ' ').replace(',', ' ')
text = text.replace(' ', ' ') # convert double space into single space
text = text.strip() # remove space at the end
tokens_without_sw = [word for word in text.split(' ') if word not in stopwords]
print(tokens_without_sw)
Result:
['Hello', 'World', 'Bye']
Example with or...
and with regex
to remove more then two spaces.
import re
stopwords = ['and', 'or']
text = "Hello World. or... Bye."
text = text.replace('.', ' ').replace(',', ' ')
#text = re.sub('\.|,', ' ', text)
#text = text.replace(' ', ' ') # convert double space into single space
text = re.sub('\s+', ' ', text) # convert many spaces into single space
text = text.strip() # remove space at the end
tokens_without_sw = [word for word in text.split(' ') if word not in stopwords]
print(tokens_without_sw)
Remove certain characters from string
You can use:
String str1 = str.replaceAll("[.]", "");
instead of:
String str1 = str.replaceAll(".", "");
As @nachokk said, you may want to read something about regex, since replaceAll
first parameter expects for a regex expression.
Edit:
Or just this:
String str1 = s.replaceAll("[,.]", "");
to make it all in one sentence.
Stripping commas and non-numeric characters from string in javascript
You should use this regex /(,|[^\d.-]+)+/g
to detect comma and any non-numeric value such as characters, operators, spaces in the groups and faster than the individual detection. a negative number (ex -1) and . will be included.
I rewrite your code.
function calcTotalRetailVal() {
var num1 = $oneTimeCostField.val();
var num2 = $recurringTotalCostField.val();
//In the replace method
var result = parseFloat(num1.replace(/(,|[^\d.-]+)+/g, '')) + parseFloat(num2.replace(/(,|[^\d.-]+)+/g, ''));
if (!isNaN(result)) {
$totalRetailAmountField.text('$' + result.toFixed(2));
}
}
Related Topics
How to Downgrade Tensorflow, Multiple Versions Possible
Package Only Binary Compiled .So Files of a Python Library Compiled With Cython
How to Repeat a Function N Times
Fastest Way to Compute Image Dataset Channel Wise Mean and Standard Deviation in Python
Parsing a Pipe-Delimited File in Python
Pip Install Not Working With Jenkins
Split Large Text File(Around 50Gb) into Multiple Files
How to Obtain Second and Fourth Word from Each Line in a File
Find Row Where Values for Column Is Maximal in a Pandas Dataframe
Valueerror: Invalid \Escape Unable to Load Json from File
Get the Mean Across Multiple Pandas Dataframes
Python - How to Fix "Valueerror: Not Enough Values to Unpack (Expected 2, Got 1)"
How to Check If a String Contains 2 of the Same Character
Why Does It Say That Module Pygame Has No Init Member
Compare Two Lists and Find the Unique Values