Java Regex to select starting and ending quotes in a string - java

I am inserting a string into a database and need to eliminate the beginning and ending quotes in the string.
I am using:
String a = rows[0].replaceAll("^\"", "").replaceAll("\"$", "");`
This gives me: "134432534"23" => 134432534"23 but is there a better way to do this?

I really do not see how your call to replace is typically bad here, but you could simplify this.
String a = rows[0].replaceAll("^\"|\"$", "");
To remove the first character and last character from the string, considering they are always quotes, use..
String a = rows[0].substring(1, rows[0].length()-1);

Related

I am trying to remove double quotes from "234521" and get the output as 234521 in java

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("^\"|\"$", "");

Java regex contained within

How can I write a regex to see if a certain string is contained within two characters.
For example, I want to see which part of the string is contained within a quotation mark.
Java"_virtual_"machine
If I run my regex through this, I want to get _virtual_
How can I achieve this using regexes?
Supposing the text contains only one such pair of characters and there are no escaping tricks or so, you can use this regex
.*"([^"]*)".*
Try it online
I wouldn't do it with regex but with method which finds first and second occurrence of a character so it's more flexible to use in the
String s = "Java\"_virtual_\"machine";
String find = "\"";
int first = s.indexOf(find) + 1;
int second = s.indexOf(find, s.indexOf(find) + 1);
s.substring(first, second);
You can reuse this code on other characters and I bet it's faster than regex.
You can use this regex. I'm not sure if this is the best regex you can use though. It will capture every string between quotes.
.*?"([^"]*)"
Demo here
If you want to get the first string that matches the regex, use :
.*?"([^"]*)".*

Java regex - How to get spaces and characters?

I am very confused on how Java's regular expressions work. I want to extract two strings from a pattern that looks like this:
String userstatus = "username = not ready";
Matcher matcher = Pattern.compile("(\\.*)=(\\.*)").matcher(userstatus);
System.out.println(matcher.matches());
But printing this will return false. I want to get the username, as well as the space that follows it, and the status string on the right of the equals sign and then store them both separately into two strings.
How would I do this? Thank you!
So the resulting strings should look like this:
String username = "username ";
String status = " not ready";
First, I assume that you are doing this as a learning exercise on regex, because a non-regex solution is easier to implement and to understand.
The problem with your solution is that you are escaping the dot, telling regex engine that you want to match literally a dot '.', not "any character". A simple fix to this problem is to remove \\:
(.*)=(.*)
Demo.
This would work, but it is not ideal. A better approach would be to say "match everything except =, like this:
([^=]*)=(.*)
Demo.
With \\.* you are trying to match zero or many dot characters. So the expression "(\\.*)=(\\.*)" actually expects something like ..=. or ..=. The wild card for any character is a simple .. To fix your code, you can change your regular expression to "(.*)=(.*)". This would match as many characters as it can before the = symbol and all the characters afterwards.
However, this solution is ugly and is not the best approach to do the job. The best thing to do is to use the split method if you want to extract what's on the left and the right side of the = sign.
You can use the split() method of a String.
String[] parts = userstatus.split("=");
String username = parts[0];
String status = parts[1];
- If its a question of fetching 2 String with = as the point of separation, then I think regex will be overkill for it.
- One can use split() method to handle this.
String lhs_Str = userstatus.split("=")[0]
String rhs_Str = userstatus.split("=")[1]

How to remove white spaces on either side of '=' sign?

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?

Getting rid of quotes in a string

Hi I have a string with quotation marks
String s = "\"hello\"";
and I was wondering how to get rid of the quotes in the string s. Is there a method that can get rid of a specific char? Something like s.getRidofChar(");
Is this what you are looking for:
s = s.replaceAll("\"", "");
The \ is because we have to escape the quotes. This is saying we will replace all instances of quotes with the empty string. Remember that String is immutable, so we need to assign the value returned by replaceAll back to s (or some other variable).
String s = "\"hello\"";
s = s.replaceAll("\"","")
Edit:
Try replaceAll method.
You can use String.replaceAll() with an empty replacement string.
If you want to remove a certain character at a certain position, take the substring before and after the character and join them together.

Categories

Resources