Java regex - How to get spaces and characters?

I am very confused about how Java regular expressions work. I want to extract two lines from a template that looks like this:

String userstatus = "username = not ready";

Matcher matcher = Pattern.compile("(\\.*)=(\\.*)").matcher(userstatus);
System.out.println(matcher.matches());

But when printed, this will return false. I want to get the username, as well as the space that follows it, and the status bar to the right of the equal sign, and then save them both separately and in two lines.

How can I do it? Thanks!

So, the resulting lines should look like this:

String username = "username ";
String status = " not ready";
+4
source share
4 answers

Firstly, I assume that you are doing this as an exercise for learning regular expressions, because a solution without regular expressions is easier to implement and understand.

, , , '.', . \\:

(.*)=(.*)

-

, . " , =, :

([^=]*)=(.*)

+3

\\.* . , "(\\.*)=(\\.*)" - ..=. ..=. .. , "(.*)=(.*)". = .

. split, , =.

+3

split() String.

String[] parts = userstatus.split("=");
String username = parts[0]; 
String status = parts[1];
+2

- 2 String = , , regex .

- You can use the method for this split().

String lhs_Str = userstatus.split("=")[0]
String rhs_Str = userstatus.split("=")[1]
0
source

Source: https://habr.com/ru/post/1614931/


All Articles