I have a csv file that looks like this:
12,2014-10-09 06:00:00,2014-10-09 06:15:00,"","","","123,456","","9,999","",""
I was able to replace the comma inbetween the digits and all double quoutes using:
String test = rowData.replaceAll("([0-9]),([0-9])","$1$2").replaceAll("\"","");
I'm not sure if this is the best approach to do this (opinion is appreciatted). My problem is I need to remove the first value before the comma also, so basically my output needs to be something like this
Orig: 12,2014-10-09 06:00:00,2014-10-09 06:15:00,"","","","123,456","","9,999","",""
Need: 2014-10-09 06:00:00,2014-10-09 06:15:00,,,,123456,,9999,,
I'm not sure if another regex is needed to do this as I don't know how exactly or use something like lastindex or firstindex to remove the fist value of the comma??? thank you
EDIT: I just noticed I can't use ([0-9]),([0-9]) cause it also remove the comma for the datetime. :(. Proper question is how to replace the csv to remove the:
1. first value
2. quotes
3. comma between the digit and quotes
Try this:
String test = rowData.replaceAll("^[^,]+|,(?!(([^\"]*\"){2})*[^\"]*$|\"(?=,)|(?<=,)\"", "");
There are three alternations that are replaced with blank (ie removed):
everything up to and including the first comma
all commas within quotes (those not followed by an even number of quotes)
all quotes adjacent to (immediately after or before) commas
To match your expected output you can do something like
String str = "12,2014-10-09 06:00:00,2014-10-09 "
+ "06:15:00,\"\",\"\",\"\",\"123,456\",\"\",\"9,999\",\"\",\"\"";
str = str.substring(str.indexOf(',') + 1);
str = str.replaceAll("\"(\\d+),(\\d+)\"", "$1$2").replace("\"", "");
String expected = "2014-10-09 06:00:00,2014-10-09 06:15:00,,,,123456,,9999,,";
System.out.println(str.equals(expected));
Output is
true
Try this
test = test.substring(test.indexOf(",") + 1, test.length());
Reasons this is better than the other guys answer: less overhead, no need for regex for this!
Related
I have a CSV file in which the values are like this:
"12342","red","world"
For processing my code which is in java, I want the double quotes to be removed and assign it to a particular variable. Like this:
String number = num.replaceAll("^\"|\"$","");
Note:quotes will always be present in starting and in the end of the value.
But the output of number is "12342" instead of 12342. What should I write to replace those double quotes?
Thanks in advance!
String number = num.replaceAll("[^\\p{IsDigit}\\p{IsAlphabetic}.,]", "");
This will work with all Strings regardless they're numbers or just text. The regex replaces everything that's not a Digit nor Alphabetic, so will remove the quotes from the CSV fields. Doesn't remove . or , neither.
Alternative should be, for just the quotes:
String number = num.replace("\"", "");
You'd need the backslash \ to escape the double-quotes.
The below regular expression can also be used.
String number= num.replaceAll("^\"|\"$", "");
Is there a way using Java String.split(regexp) to split on strings inside of quotes, and not get the quotes?
The strings I have to deal with are like the following. I don't have control of the format and the number of strings are variable:
"strA" : "strB" : "strC" : "strD",
"strE" : "strF" : "strG",
Note: The spaces are included, and each line is handled separately.
So what I would like to get is an array with all strings.
I could use replaceAll to strip the quotes, spaces and commas, then split on the colon:
line = line.replaceAll(/(\"|,\\s+)/,"");
usrArray = line.split(":");
But I'd like to do this with one regexp.
This should do the trick.
usrArray = line.split("(\" : \")|(\",?)");
This looks first for " : ". If it doesnt find that it will look for the edge cases, " and ",. If you need it to also search for newlines, use this regex.
usrArray = line.split("(\" : \")|(\",?\n?)");
I have strings like
#lle #mme: #crazy #upallnight:
I would like to remove the words which starts with either # or #. It works perfectly fine if those words doesn't contain the ':' character. However, that ':' character is left whenever I delete the words. Therefore I decided to replace those ':' characters before I delete the words using a string.replace() function. However, they are still not removed.
String example = "#lle #mme: #crazy #upallnight:";
example.replace(':',' ');
The result : #lle #mme: #crazy #upallnight:
I am pretty stuck here, anyhelp would be appreciated.
You can do this:
example = example.replaceAll(" +[##][^ ]+", "");
What this will do is replace any substrings in your string that match the regex pattern [##][^ ]+ with the empty string. Since that pattern matches the words you want to dump, it'll do what you want.
Demo of the pattern on Regex101
From Java docs:
String s = "Abc: abc#:";
String result = s.replace(':',' ');
Output in variable result= Abc abc#
I think you forgot to store the returned result of replace() method in some other String variable.
I have the following string String string = "attr1 = 45 attr2 =\"82\"";
I am trying to remove all whitespace characters on either side of the = sign.
So that for example my output looks like:
attr1=45 attr="82"
I have tried the following:
String string = "attr1 = 45 attr2 =\"82\"";
string = string.replaceAll("\\s+", "");
I get the following output: attr1=45attr2="82"
Any suggestions would be appreciated.
You don't want to replace all spaces, but only these that are around =. Try with
string = string.replaceAll("\\s*=\\s*", "=");
Note that you cannot do this with just one regex since regexes are designed to match strings, not modify them. Regexes are often used with other tools to perform the later task. In particular, you can use String.replace() or String.replaceAll() with a very simple regex to accomplish your task.
Edit:
If you are still stumped, step back for a minute and think: How would you replace an equal sign with an asterisk, for example? Now can you modify that to do what you actually want?
I am reading each line in the text file, do some with the file and write back to same file using Java. And, position(index) of each value in the line is important. So I need to preserve the location of the each value.
How do I remove a "space" character at specific location(index)?
Say, below is the line that I read,
.... ABC 123.... --There are 3 spaces between ABC and 123
Basically, I want to make the above line written as 2 spaces between ABC and 123.
At first, I was just using replaceAll of String but that just shift the the values to right by one and still 3 spaces. So, I figure I need more than just replaceAll.
Now, I am getting a position of where ABC is found and just trying to remove that 1 space.
If you want to remove a single character, just use StringBuilder.deleteCharAt.
final int pos = ...;
str = new StringBuilder(str).deleteCharAt(pos).toString();
I explicitly advise you not to do the substring approach.
Can you not do a search for the whole string? The first parameter has three spaces and the second has two spaces
replaceAll("ABC 123", "ABC 123");
or if ABC is your key input
replaceAll("ABC ", "ABC ");
and the first parameter has 3 spaces and the second two after the ABC
Since you know the position n (zero based) of string s that you want to remove:
s = s.substring(0, n) + s.substring(n + 1);
You could just convert your string to a char array and loop it, but it's not a nice way to do it.
Or you could do:
myString = myString.substring(0,(position of first space))+
myString.substring((position of first space)+1, myString.length);
or something like that