How to Remove Comma and Brackets

Removing Commas and Square Brackets from Stack or String in Java Android Studio

The reason you're still seeing the old value is because Strings are immutable (once they've been created, they don't change.)

The solution is to set your final_result variable to the result of your replaceAll method call. Strange, I know.

Like this: final_result = final_result.replaceAll(",", "");

p.s. You'll need to replace the spaces and the brackets too, but you'll figure that out :)

Regex for removing Commas within brackets

You can use this regex which uses positive look ahead to discard matching a comma that is outside the parenthesis,

,(?=[^()]*\))

Demo

var s = "(37.400809377128354, -122.05618455969392),(37.3931723332768, -121.89276292883454)";console.log(s.replace(/,(?=[^()]*\))/g, ''));

How do I remove square brackets and comma's from a text file?

Try:

$ sed -e 's/\[//g' -e 's/\]//g' -e 's/,//g' file
371590146 2019-04-28 123.2
371712941 2019-04-29 128.72
371828179 2019-04-30 148.35

or:

$ sed -e 's/[][,]//g' file
371590146 2019-04-28 123.2
371712941 2019-04-29 128.72
371828179 2019-04-30 148.35

or:

$ sed -Ee 's/\[|\]|,//g' file
371590146 2019-04-28 123.2
371712941 2019-04-29 128.72
371828179 2019-04-30 148.35

Note that [ and ] are regex-active characters. If you want them to be treated literally as square brackets, they should be escaped with \. (Sometimes a program is smart enough to know that you meant it literally, as in the code in the question, but it is best practice not to count on that.)

[][,] means any of ], [, or ,. [...] is called a bracket expression. It matches any character contained within the brackets.

\[|\]|, also means any of ], [, or ,. In extended regular expressions (-E option), the character | separates branches. This matches if the regex on either side of the | matches.

How to remove square brackets and commas from dataframe column

Convert to string and use str.repalce with regex:

df = pd.DataFrame({'i1': [1.0]*2, 'i2': [1.0]*2,
'R1': [[0.0]*3]*2, 'R2': [[0.0, 0.0, 0.802],[0.0, 0.0, -0.802]],
'D': [-0.013347]*2})

df['R1'] = df['R1'].astype(str).str.replace(r'\[|\]|,', '')
df['R2'] = df['R2'].astype(str).str.replace(r'\[|\]|,', '')

i1 i2 R1 R2 D
0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.802 -0.013347
1 1.0 1.0 0.0 0.0 0.0 0.0 0.0 -0.802 -0.013347

Javascript/regex: Remove comma only if it is not in brackets

This is the regex you want: /,\s*(?=[^)^\]]*(?:\(|\[|$))/g

Here's the js fiddle replacing your string commas outside [] and () for & using String.prototype.replace():

var string = 'filters=fuelType=D,make=[BMW,CITROEN,DACIA],price=(0,100)';
var result = string.replace(/,\s*(?=[^)^\]]*(?:\(|\[|$))/g, '&');
alert(result); // -> `filters=fuelType=D&make=[BMW,CITROEN,DACIA]&price=(0,100)`

https://jsfiddle.net/tmms2mck/

And here's the regex101 explanation of this regular expression: https://regex101.com/r/hAuEQm/1

How to remove unwanted characters (brackets, quotes, and commas) from a JSON string?

You already have your JSON data in a string, so you need to parse it into a JavaScript object (an array in your case):

JSON.parse(jsonString)

This will give you a data structure that you can iterate over, to extract each item in a clean way.

Here is a demo:

var jsonString = '[ "Monday: 2:00 – 10:00 PM", "Tuesday: 2:00 – 10:00 PM", "Wednesday: 2:00 – 10:00 PM", "Thursday: 2:00 – 10:00 PM", "Friday: 12:00 – 11:00 PM", "Saturday: 12:00 – 11:00 PM", "Sunday: 12:00 – 9:00 PM" ]';

var businessHours = JSON.parse(jsonString);

businessHours.forEach((bizDay) => {
console.log( bizDay );
} );

regex to remove comma and space inside of square brackets

It looks like you want to

  • remove all nulls
  • replace number, number, number with number:number:number, in other words replace each , which has digit before it with :
  • remove [ and ]

Demo:

String input = 
"year.\", [229, 338], null, [1144, 2371]\r\n" +
"year....\", null, null, [812]\r\n" +
"year:, null, null, [1019, 1028, 2463]\r\n" +
"year;, null, [164], [1052]";

String expected =
"year.\", 229:338, , 1144:2371\r\n" +
"year....\", , , 812\r\n" +
"year:, , , 1019:1028:2463\r\n" +
"year;, , 164, 1052";

input = input.replace("null", "")
.replaceAll("(?<=\\d), ", ":")
.replaceAll("\\[|\\]", "");

System.out.println(input.equals(expected));

Output: true.

How to remove brackets, quotes, and add a space after each comma for a const - javascript

You don't need or want a regular expression for this. What you're getting from _.map is an array of strings. To get the result you want:

  1. In your map callback, do the capitalization of key.

  2. Use .join(", ") on the resulting array to join all entries into one string using ", " as the delimiter

You can also swap the if/else for the conditional operator if you like. Something like:

const result = _.map(daysOfWeek, function(value, key) {
return key.substring(0, 1).toUpperCase() +
key.substring(1) +
": " +
(value ? "Y" : "N");
}).join(", ");

or with an arrow function and a template literal

const result = _.map(daysOfWeek, (value, key) =>
`${key.substring(0, 1).toUpperCase()}${key.substring(1)}: ${value ? "Y" : "N"}`
).join(", ");

or with iterable destructuring, an arrow function, and a template literal:

const result = _.map(daysOfWeek, (value, [first, ...rest]) =>
`${first.toUpperCase()}${rest.join("")}: ${value ? "Y" : "N"}`
).join(", ");

though that involves a temporary array.

How do I remove comma and brackets

import re 
ans_con=(str(d))
print re.sub(r'\W', '', ans_con)


Related Topics



Leave a reply



Submit