Reputation: 1809
I have the following string in Java.
ActivityRecord{7615a77 u0 com.example.grano.example_project/.MainActivity t20}
My need is to get the string MainActivity
, ie the part between the ./
and the space after the word.
So basically I'm looking for a regular expression able to catch something in the middle of given characters and a white space.
Upvotes: 0
Views: 1341
Reputation: 44150
You can use the expression:
(?<=\/\.)\w+?(?=\s)
Broken down:
(?<= \/\. )
^ lookbehind
^ for a literal / followed by a literal .
\w +?
^ word character
^ one or more (non-greedy)
(?= \s )
^ lookahead
^ a whitespace character
Upvotes: 1
Reputation: 59978
You can use a regex like this /\.(.*?)\s
with pattern like :
String str = ...;
String regex = "/\\.(.*?)\\s";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
//-------------------------------^-----get the group (.*?) between '/.' and 'space'
}
Output
MainActivity
Upvotes: 1
Reputation: 940
Assuming your proceeding text doesn't have / in it and the text you want to isolate doesn't have a space in it, you can use this
replaceAll("^[^/]*/\\.([^ ]*).*$","$1"));
which looks from the start for the first /, then /., then captures everything up to the first space from that point, and then matches everything else, and replaces it all with the capture
Upvotes: 1