Reputation: 3436
I have a string line
String user_name = "id=123 user=aron name=aron app=application";
and I have a list that contains: {user,cuser,suser}
And i have to get the user part from string. So i have code like this
List<String> userName = Config.getConfig().getList(Configuration.ATT_CEF_USER_NAME);
String result = null;
for (String param: user_name .split("\\s", 0)){
for(String user: userName ){
String userParam = user.concat("=.*");
if (param.matches(userParam )) {
result = param.split("=")[1];
}
}
}
But the problem is that if the String contains spaces in the user_name
, It do not work.
For ex:
String user_name = "id=123 user=aron nicols name=aron app=application";
Here user
has a value aron nicols
which contain spaces. How can I write a code that can get me exact user
value i.e. aron nicols
Upvotes: 0
Views: 140
Reputation: 124215
If you want to split only on spaces that are right before tokens which have =
righ after it such as user=...
then maybe add look ahead condition like
split("\\s(?=\\S*=)")
This regex will split on
\\s
space (?=\\S*=)
which has zero or more *
non-space \\S
characters which ends with =
after it. Also look-ahead (?=...)
is zero-length match which means part matched by it will not be included in in result so split will not split on it. Demo:
String user_name = "id=123 user=aron nicols name=aron app=application";
for (String s : user_name.split("\\s(?=\\S*=)"))
System.out.println(s);
output:
id=123
user=aron nicols
name=aron
app=application
From your comment in other answer it seems that =
which are escaped with \
shouldn't be treated as separator between key=value
but as part of value. In that case you can just add negative-look-behind mechanism to see if before =
is no \
, so (?<!\\\\)
right before will require =
to not have \
before it.
BTW to create regex which will match \
we need to write it as \\
but in Java we also need to escape each of \
to create \
literal in String that is why we ended up with \\\\
.
So you can use
split("\\s(?=\\S*(?<!\\\\)=)")
Demo:
String user_name = "user=Dist\\=Name1, xyz src=activedirectorydomain ip=10.1.77.24";
for (String s : user_name.split("\\s(?=\\S*(?<!\\\\)=)"))
System.out.println(s);
output:
user=Dist\=Name1, xyz
src=activedirectorydomain
ip=10.1.77.24
Upvotes: 5
Reputation: 784898
Do it like this:
First split input string using this regex:
" +(?=\\w+(?<!\\\\)=)"
This will give you 4 name=value
tokens like this:
id=123
user=aron nicols
name=aron
app=application
Now you can just split on =
to get your name and value parts.
Upvotes: 4
Reputation: 41838
CODE FISH, this simple regex captures the user in Group 1: user=\\s*(.*?)\s+name=
It will capture "Aron", "Aron Nichols", "Aron Nichols The Benevolent", and so on.
It relies on the knowledge that name=
always follows user=
However, if you're not sure that the token following user is name, you can use this:
user=\s*(.*?)(?=$|\s+\w+=)
Here is how to use the second expression (for the first, just change the string in Pattern.compile
:
String ResultString = null;
try {
Pattern regex = Pattern.compile("user=\\s*(.*?)(?=$|\\s+\\w+=)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
ResultString = regexMatcher.group(1);
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
Upvotes: 1