Wandy Tang wrote
> I would like to extract string from this situation,
> @phone:12345678@ or @phone:123456789@.
> How can I retrieve string from between [@phone:@]?
> I do not want to read character one by one,
> I am guessing I can use regular expression to achieve this
situation...
Indeed you can use regular expressions. For instance, the following
class will return the phone numbers from 4 to 10 digits long, or null if
there is no match.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PhoneMatcher {
private final Pattern pattern = Pattern.compile("@phone

\\d{4,10})@");
public String getPhone(String input) {
Matcher matcher = pattern.matcher(input);
return matcher.matches() ? matcher.group(1) : null;
}
}
Read more about regex in the javadoc for Pattern and Matcher.
Regards,
--
Filip Larsen