这是什么意思?请提供更多上下文。

3
在这段代码中,我们使用 .replaceFirst 方法从 email 字符串中删除子字符串 "luna"。我们在 + 和 @ 中间删除字符。但是由于使用的是 .replaceFirst 方法,所以这仅会发生在第一个实例中。如果我们想要定位到第二个 + 和 @ 实例并删除 "smith",该怎么办呢? 我们现在的输出是 alice+@john+smith@steve+oliver@,但我们希望得到 alice+luna@john+@steve+oliver@
public class Main {

    public static void main(String[] args) {

        String email = "alice+luna@john+smith@steve+oliver@";

        String newEmail = email.replaceFirst("\\+.*?@", "");

        System.out.println(newEmail);

    }
}

1
使用正则表达式来做这个? - triplem
4个回答

2
您可以这样找到第二个+
int firstPlus = email.indexOf('+');
int secondPlus = email.indexOf('+', firstPlus + 1);

如果必要的话,您需要处理没有两个+的情况。

然后找到以下@

int at = email.indexOf('@', secondPlus);

然后将它重新拼接起来:
String newEmail = email.substring(0, secondPlus + 1) + email.substring(at);

或者

String newEmail2 = new StringBuilder(email).delete(secondPlus + 1, at).toString();

Ideone demo


1

不幸的是,Java 没有像 replace second、replace third 等方法。你可以使用 replaceAll(它会替换所有出现的字符串)或者在已经替换过的字符串上再次调用 replaceFirst。这基本上就是替换第二个。如果只想替换第二个 - 那么可以使用子字符串或者在结果上进行正则匹配并进行迭代。

  public static void main(String[] args) {

        String email = "alice+luna@john+smith@steve+oliver@";

        String newEmail = email.replaceFirst("\\+.*?@", "");
        newEmail = newEmail .replaceFirst("\\+.*?@", ""); //this replaces the second right? :) 
        newEmail = newEmail .replaceFirst("\\+.*?@", ""); // replace 3rd etc.

        System.out.println(newEmail);

    }

0
您可以在以下的replaceNth方法中交替更改参数n的值为2、3,以执行与replaceSecondreplaceThird完全相同的操作。(注意:此方法可应用于n的任何其他值。如果第n个模式不存在,则它将简单地返回给定的字符串)。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static String replaceNth(String str, int n, String regex, String replaceWith) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        while (m.find()) {
            n--;
            if (n == 0) {
                return str.substring(0,m.start() + 1)+ replaceWith + str.substring(m.end() - 1);
            }
        }
        return str;
    }

    public static void main(String args[]) {
        String email = "alice+luna@john+smith@steve+oliver@";
        System.out.println(replaceNth(email, 2, "\\+.*?@", ""));
    }
}

0

我认为将这个逻辑封装到一个单独的方法中会更好,其中String组位置是参数。

private static final Pattern PATTERN = Pattern.compile("([^+@]+)@");

private static String removeSubstringGroup(String str, int pos) {
    Matcher matcher = PATTERN.matcher(str);

    while (matcher.find()) {
        if (pos-- == 0)
            return str.substring(0, matcher.start()) + str.substring(matcher.end() - 1);
    }

    return str;
}

此外,您还可以添加更多的方法来简化使用这个工具;例如removeFirst()removeLast()

public static String removeFirst(String str) {
    return removeSubstringGroup(str, 0);
}

public static String removeSecond(String str) {
    return removeSubstringGroup(str, 1);
}

演示:

String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(email);
System.out.println(removeFirst(email));
System.out.println(removeSecond(email));
System.out.println(removeSubstringGroup(email, 2));
System.out.println(removeSubstringGroup(email, 3));

输出:

alice+luna@john+smith@steve+oliver@
alice+@john+smith@steve+oliver@
alice+luna@john+@steve+oliver@
alice+luna@john+smith@steve+@
alice+luna@john+smith@steve+oliver@

Ideone演示


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接