Extract few substrings from one string using a single regex

As an input I get a string like "0123456789@site.com". All parts are variable. Only rules are that the number of digits in front is always 10 and then there is "@". I need a regex which will allow me to extract "12345" (ie digits from positions 2 to 6) and "site.com" substrings. For example, in above case the result could be either "12345site.com" or "12345:site.com". Can it be done with a single regular expression? How can we skip first digit and digits from positions 7 to 10 and '@'? Examples in Java will be appreciated.


If i understood you correctly, this regex will do

d(d{5})d{4}@(.+)

and then use

matcher.group(1) + matcher.group(2)

to concatenate the groups.

Java code:

public static void main(String[] args) {
    String s = "0123456789@site.com";
    String patternString = "d(d{5})d{4}@(.+)";
    Pattern pattern = Pattern.compile(patternString);
    Matcher matcher = pattern.matcher(s);
    if (matcher.matches()) {
        System.out.println(matcher.group(1) + matcher.group(2));
        // shows "12345site.com"
    }
}

特别针对您的输入模式:

d{1}(d{5})d*@(.*)

2 capturing groups: 
   group 1: (d{5})
   group 2: (.*)

Input: 0123456789@site.com
Output: 12345
        site.com
链接地址: http://www.djcxy.com/p/15258.html

上一篇: 确定RGB颜色亮度的公式

下一篇: 使用单个正则表达式从一个字符串中提取少量子字符串