Created
August 15, 2011 11:51
-
-
Save dahernan/1146091 to your computer and use it in GitHub Desktop.
CharMatcher example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testting.testinggggg; | |
import java.util.List; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import com.google.common.base.CharMatcher; | |
import com.google.common.base.Splitter; | |
import com.google.common.collect.Collections2; | |
import com.google.common.collect.Iterables; | |
import com.google.common.collect.Lists; | |
/** | |
* Hello world! | |
* | |
*/ | |
public class App { | |
public static void main( String[] args ){ | |
System.out.println("testing world!!"); | |
String test1 = "{key:core.localizations.search} {key:configuration}"; | |
String test2 = "{key:core.localizations.search}{key:configuration}"; | |
String test3 = "blablaneggpajga eagagaega {key:core.localizations.search} afagadg afafasfa{key:configuration}afafga fasaf"; | |
extractKeys(test1); | |
extractKeys(test2); | |
extractKeys(test3); | |
System.out.println("finish :)"); | |
} | |
public static Iterable<String> extractKeys(String target) { | |
String regexp = "\\{\\s*(((?!\\{|\\}).)+)\\s*\\}"; | |
List<String> result = Lists.newArrayList(); | |
Pattern p = Pattern.compile(regexp); | |
Matcher m = p.matcher(target); // get a matcher object | |
int count = 0; | |
while(m.find()) { | |
String expression = m.group(); | |
CharMatcher charMatcher = CharMatcher.anyOf("{}"); | |
String trimmed = charMatcher.replaceFrom(expression, " "); | |
Iterables.addAll(result, Splitter.on(CharMatcher.WHITESPACE).trimResults().omitEmptyStrings().split(trimmed.replace("key:", ""))); | |
} | |
// test | |
for (String value : result) { | |
System.out.println("=>" + value); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment