如何在Java中分离字符、数字和符号?

4

我有一个问题,我有一些类似于这样的字符串

تاپقان بولۇپ، توپلامغا 1998 – يىلىدىن 2009يىلىغىچە شىنجاڭ

是的,它们是用维吾尔语书写的,就像阿拉伯语一样,而我不懂维吾尔语。

现在我需要按空格、符号和数字将它们分开。我尝试使用Python,并且我可以得到这个结果。

تاپقان   بولۇپ ،    توپلامغا      1998       –    يىلىدىن      2009   يىلىغىچە   شىنجاڭ

如果我忽略了很多空格,那么结果就是我想要的。而Python代码如下:
def re_str(matched):
    replace_str = matched.group('symbol')
    return ' ' + replace_str + " "
# test is the string above
print(re.sub('(?P<symbol>\W)', re_str, re.sub('(?P<symbol>\d+)', re_str, test)))

现在的问题是:我想用Java实现这个效果,但是我不知道怎么做?请帮助我。 我尝试使用Java,但是它没有起作用。
String pattern = "(\\d+)|([\\p{P}\\p{S}]+)|\\W";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(test);

你在Java方面尝试过什么?你有一些Python的经验,所以很多东西会相似。你只需要弄清语法和字符串替换的差异即可。基本上,没有人会为你编写这段代码。 - user1531971
1
看一下Java正则表达式,特别是PatternMatcher类。与它们相关的Javadocs应该已经包含了很多信息。 - Thomas
谢谢您的回复,我刚学Java,不知道如何在Java中实现像Python中的re.sub函数。@jdv - zyethan
谢谢,我已经查看了信息,并尝试使用Patter和Matcher,但似乎不起作用。@Thomas - zyethan
如果后面的代码片段是你整个Java代码,那么你缺少实际的字符串分割部分。你要找的方法在Pattern类上,它的完整签名是String[] split(CharSequence)。还有一个有用的splitAsString方法,你可能想要研究一下。或者,你可以直接调用String类的split方法:它接受一个正则表达式作为输入参数。 - M. Prokhorov
2个回答

0
我写了一个函数,你应该能够做到这一点,但我不确定你想要哪些符号,所以你需要修改SYMBOL_MATCHER_REGEX来匹配你要查找的任何符号。 $0是与模式匹配的结果的引用,该函数只是将匹配项替换为自身,并添加制表符前后。
  /**
   * The regex used to find any symbols you are looking for.
   */
  private String SYMBOL_MATCHER_REGEX = "[0-9]+";

  /**
   * A replacement which adds space before and after the match.
   */
  private String REPLACEMENT_STRING = "   $0    ";

  /** 
   * Compiled pattern for the SYMBOL_MATCHER_REGEX. 
   */
  private Pattern SYMBOL_PATTERN = Pattern.compile(SYMBOL_MATCHER_REGEX);

  public String formatUyghur(String uyghurText) {
    Matcher matcher = SYMBOL_PATTERN.matcher(uyghurText);

    return matcher.replaceAll(REPLACEMENT_STRING);
  }

0
使用isAlphabeticisDigit的组合,否则你就有一个特殊字符。
public class Separater {

static String splitString(String str) {
    String result = "";
    int i=0;
    while (i < str.length()) {//Using while instead of for, to avoid skipping characters due to auto increment by the loop.

        if (Character.isDigit(str.charAt(i))) {
            while (i < str.length() && Character.isDigit(str.charAt(i))) {
                result += str.charAt(i);
                i++;
            }
            result += "     ";
        } else if (Character.isAlphabetic(str.charAt(i))) {
            while (i < str.length() && Character.isAlphabetic(str.charAt(i))) {
                result += str.charAt(i);
                i++;
            }
            result += "     ";
        } else {
            while (i < str.length() && !Character.isAlphabetic(str.charAt(i)) && !Character.isDigit(str.charAt(i))) {
                result += str.charAt(i);
                i++;
            }
            result += "     ";
        }
    }
    return result;
}

public static void main(String[] args) {
    System.out.println(splitString("تجاؤي#*(اىيلاؤت678345شسسصي*&&*^*!!محجذلب"));
}
}

输出 请注意,子字符串之间的空格更大,但SO会删除额外的空格!

تجاؤي #*( اىيلاؤت 678345 شسسصي &&^*!! محجذلب


1
太棒了!它对我有效,谢谢你,我被正则表达式困住了。 :) - zyethan

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