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

如何在Java中分隔字符、数字和符号?,java,Java,我有个问题,我有一些字符串,像这样 تاپقان بولۇپ، توپلامغا 1998 – يىلىدىن 2009يىلىغىچە شىنجاڭ 是的,他们是用维语写的,很像阿拉伯语,我不懂维语 我现在需要用空格、符号和数字来分隔它们。 我用python尝试了一下,我可以得到这个结果 تاپقان بولۇپ ، توپلامغا 1998 – يىلىدىن 2009 يىلىغىچە شىنجاڭ 如果我忽略了很多空格,结果就

我有个问题,我有一些字符串,像这样

تاپقان بولۇپ، توپلامغا 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)))

我做了一个函数,你应该能够做到这一点,我不确定你到底想要哪个符号,所以你必须修改符号匹配器正则表达式,以匹配你正在寻找的任何符号

$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);
  }

使用
isAlphabetic
isDigit
的组合,否则您将具有特殊字符

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شسسصي*&&*^*!!محجذلب"));
}
}
公共类分隔符{
静态字符串拆分字符串(字符串str){
字符串结果=”;
int i=0;
while(i
输出 请注意,子字符串之间的空间较大,但会删除额外的空间

ت


您在Java中尝试过什么?您对Python有一定的经验,因此很多方面都是相似的。您只需整理一下语法和字符串替换工作方式的差异。基本上,没有人会为您编写此代码。请看一下Java正则表达式,特别是类
模式
匹配器
 。与这些相关联的Javadocs应该已经包含了很多信息。谢谢您的回复,我刚刚学习了Java,我不知道如何在Java中用python实现re.sub之类的函数。@jdvThank,我检查了信息,尝试使用Patter和Matcher,但似乎不起作用。@ThomasIf后一段代码是您的全部Java代码,然后,您将丢失字符串部分的实际拆分。您要查找的方法位于模式类上,其完整签名为
string[]split(CharSequence)
。这里还有一个有用的
splitAsString
方法,您可能希望了解。或者,您可以简单地调用
String
类的
split
方法:它接受正则表达式作为输入参数。太好了!它适合我,谢谢,我被困在正则表达式中了。:)
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شسسصي*&&*^*!!محجذلب"));
}
}