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

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?

Related

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]

Java Regex to select starting and ending quotes in a string

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);

How to use `regex` to trim a parsed String?

I could have used Decimal Format, but I must use regex in this case.
I have the following String myString = "0.44587628865979384"; I need to trim it to three decimal places, so that is looks like 0.445
I tried the following:
String myString = "0.44587628865979384";
String newString = myString.replaceFirst("(^0$.)(\\d{3})(\\d+)","$1$2");
// But this does not work. What is the problem in here?
It's not (^0$.). It's (\\d*.)
String newString = myString.replaceAll("(\\d*.)(\\d{3})(\\d+)", "$1$2");
Well, one suitable pattern could be "^\d\.\d{3}", though you will have to do a match to retrieve it (i.e. you're fetching what you need, not replacing what you don't need as in your example).
... but, why would you use regex for this job?
Regex is used to find common patterns in text and lets you extract/remove/list them. It is not intended to deal with string measurement
What you need is to slice the string into a subset of characters using the substring method:
myString.substring(5);
If the position of the decimal point varies:
myString.substring(myString.indexOf(".") + 4);
Remember: "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." (http://www.codinghorror.com/blog/2008/06/regular-expressions-now-you-have-two-problems.html)
Try this:
String newString = myString.replaceAll"(.*\\....).*", "$1");

need to clean malformed tags using regular expression

Looking to find the appropriate regular expression for the following conditions:
I need to clean certain tags within free flowing text. For example, within the text I have two important tags: <2004:04:12> and <name of person>. Unfortunately some of tags have missing "<" or ">" delimiter.
For example, some are as follows:
1) <2004:04:12 , I need this to be <2004:04:12>
2) 2004:04:12>, I need this to be <2004:04:12>
3) <John Doe , I need this to be <John Doe>
I attempted to use the following for situation 1:
String regex = "<\\d{4}-\\d{2}-\\d{2}\\w*{2}[^>]";
String output = content.replaceAll(regex,"$0>");
This did find all instances of "<2004:04:12" and the result was "<2004:04:12 >".
However, I need to eliminate the space prior to the ending tag.
Not sure this is the best way. Any suggestions.
Thanks
Basically, you are looking for a negative look-ahead, like this:
String regex = "<\\d{4}-\\d{2}-\\d{2}(?!>)";
String output = content.replaceAll(regex,"$0>");
This will help with the numeric "tags", but since no regex can be intelligent enough to match an arbitrary name, you either must define very closely what a name can look like, or deal with the fact that the same approach is impossible for "name" tags.
For fixing the dates, you can match any date, with zero one or two angled brackets:
String regex = "(\\s?\\<?)(\\d{4}:\\d{2}:\\d{2})(\\>?\\s)";
String replace = " <$2> ";
To recognise a name, we assume parts of the name begin with a capital letter and the only separator is a space. We match the angled bracket explicitly at the start or end, and the preceeding/succeeding char before/after the name should be only a space or punctuation.
String regex = "(\\<[A-Z][a-zA-Z]*(\\s[A-Z][a-zA-Z])*)(?=[\\.!?:;\\s])";
String replace = "$1>";
String regex = "(?<=[\\.!?:;\\s])([A-Z][a-zA-Z]*(\\s[A-Z][a-zA-Z]*)*)";
String replace = "<$1";

Categories

Resources