I would like to look for any word in a file containing different informations like date and percentage and some strings.
Input:
21-02-2015 wordA 22 wordB wordC
Result:
wordA wordB wordC
Please help me for I am new to regex.
I would like to look for any word in a file containing different informations like date and percentage and some strings.
Input:
21-02-2015 wordA 22 wordB wordC
Result:
wordA wordB wordC
Please help me for I am new to regex.
This is the regex that could retrieve any string including special character answer found it :
(([a-zA-Z]+)(\W)?([a-zA-Z]+))
([a-zA-Z]+)
Look for character in Aa-zZ(\W)
look for the special character ([a-zA-Z]+)
if the special character is in the middle you look for the rest of the wordJava's regex implementation supports character class intersection, for which this is a textbook usecase.
[\w&&[^\d]]
will thus match a word character but not a digit. Together with Pattern.UNICODE_CHARACTER_CLASS
it should match ‘special characters’.
Thus this code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class test {
public static void main(String... args) {
String input = "21-02-2015 wordA 22 wordB wordC Førtids";
Matcher matcher = Pattern.compile("[\\w&&[^\\d]]+",
Pattern.UNICODE_CHARACTER_CLASS)
.matcher(input);
while (matcher.find() ) {
System.out.println(matcher.group() );
}
}
}
Produces:
wordA
wordB
wordC
Førtids