Java regex - How to get spaces and characters? - java

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]

Related

Split String if it has number

Hi Guys its been a while since I ask another question,
I have this String which consist of a name and a number
Ex.
String myString = "give11arrow123test2356read809cell1245cable1257give222..."
Now what I am trying to do is to split it whenever there is a number attached to it
I have to split it so that I could have a result like this
give11, arrow123, test2356, read809, cell1245, cable1257, give222, ....
I could use this code but I cant find the right regex
String[] arrayString = myString.split("Regex")
Thanks for your help.
You can use a combination of lookarounds to split your string.
Lookarounds are zero-width assertions. They don't consume any characters on the string. The point of zero-width is the validation to see if a regex can or cannot be matched looking ahead or looking back from the current position, without adding them to the overall match.
String s = "give11arrow123test2356read809cell1245cable1257give222...";
String[] parts = s.split("(?<=\\d)(?=\\D)");
System.out.println(Arrays.toString(parts));
Output
[give11, arrow123, test2356, read809, cell1245, cable1257, give222, ...]
Use this regex for spliting
String regex = "(?<=\\d)(?=\\D)";
I am unfamiliar with using regex in java, but this expression matches what you need on www.rubular.com
([A-Za-z]+[0-9]+)

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?

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

String replaceAll with conditions

I am not good in regular expressions and I need help in replacing the string.
String str = "Name_XYZ_";
str = "XYZ_NAME_";
So how can I replace "Name_" or "_NAME_" from above two strings with empty string?
The conditions are "Name" can be in any case and it can be at index 0 or at any index but preceded by "_".
So far I tried,
String replacedString = str.replaceAll("(?i)Name_", ""); // This is not correct.
This is not the homework. I am working on XML file that needs such kind of processing.
String replacedString = str.replaceAll("(?i)(?:^|_)name_", "");
You were close. What you have to do is either anchor name to the beginning of the string (with ^) or require an underscore there. I also changed Name to name, because why mix lower and upper case, if you are treating the pattern case-insenstively anyway. Note that ?: is just an optimization (and a good practice). It suppresses capturing which you don't need in this case.
If you want to improve your regex skills, I can highly recommend this tutorial.
I'm using .NET's regex instead of Java's, but in that context (_?Name_) should work.

How to make regex matching fail if checked string still has leftover characters?

I'm trying to check a string with a regular expression, and this check should only pass if the string contains only *h, *d, *w and/or *m where * can be any number.
So far I've got this:
Pattern p = Pattern.compile("([0-9]h)|([0-9]d)|([0-9]w)|([0-9]m)");
Matcher m = p.matcher(strToCheck);
if(m.find()){
//matching succesful code
}
And it works to detect if there are any of the number-letter combinations present in the checked string, but it also works if the input is, for instance, "12x5d", because it has "5d" in it. I don't know if this is a code problem or a regex problem. Is there a way to achieve what I want?
EDIT:
Thank you for your answers so far, but as requested, I'll try to clarify a bit. A string like "1w 2d 3h" or "1w 1w" is valid and should pass, but something like "1w X 2d 3h", "1wX 2d" or "w d h" should fail.
use m.matches() or add ^ and $ to the beginning and end of the regex resp.
edit but if you wan sequences of these delimited by whitespace (as mentioned in the comments) you can use
Pattern.compile("\\b\\d[hdwm]\\b");
Matcher m = p.matcher(strToCheck);
while(m.find()){
//matching succesful code
}
Firstly, I think you should use matches() instead of find(). The former matches the entire string against the regex, whereas the latter searches within the string.
Secondly, you can simplify the regex like so: "[0-9][hdwm]".
Finally, if the number can contain multiple digits, use the + operator: "[0-9]+[hdwm]"
try this:
Pattern p = Pattern.compile("[0-9][hdwm]");
Matcher m = p.matcher(strToCheck);
if(m.matches()){
//matching succesful code
}
If you want to only accept things like 5d as a complete word, rather than just part of one, you can use the \b "word border" markers in regex:
Pattern p = Pattern.compile("\\b([0-9]h)|([0-9]d)|([0-9]w)|([0-9]m)\\b");
This will let you match a string like "Dimension: 5h" while rejecting a string like "Dimension: 12wx5h".
(If, on the other hand, you only want to match if the entire string is just 5d or the like, then use matches() as others have suggested.)
You can write it like this "^\\d+[hdwm]$". Which should only match on the desired strings.

Categories

Resources