正则表达式用于分割每个字符但将数字保持在一起

时间:2015-12-05 10:43:20

标签: java regex

我正在寻找一个将字符串拆分的正则表达式,如下所示:

String input = "x^(24-3x)";
String[] signs = input.split("regex here");
for (int i = 0; i < signs.length; i++) { System.out.println(sings[i]); }

输出结果为:

"x", "^", "(", "24", "-", "3", "x", ")"

字符串在每个字符处分开。但是,如果彼此相邻,则它们应保持分组在一个字符串中。

2 个答案:

答案 0 :(得分:4)

您可以使用此基于外观的正则表达式:

String[] signs = input.split("(?<!^)(?=\\D)|(?<=\\D)");

RegEx Demo

RegEx分手

(?<!^)(?=\\D)  # assert if next char is non-digit and we're not at start
|              # regex alternation
(?<=\\D)       # assert if previous character is a non-digit

答案 1 :(得分:0)

你也可以使用模式和匹配器分成令牌,这是相当可读的

String regex="\\d+|[a-z]+|[\\-()\\^]";
String  str="x^(24-3x)";

如果使用str =“xxx ^(24-3xyz)”;

也很容易

要获得所有代币,这有点棘手:

我用这个:

礼貌: Create array of regex matches

for (MatchResult match : allMatches(Pattern.compile(regex), str)) {
  System.out.println(match.group() + " at " + match.start());
}

public static Iterable<MatchResult> allMatches(
      final Pattern p, final CharSequence input) {
  return new Iterable<MatchResult>() {
    public Iterator<MatchResult> iterator() {
      return new Iterator<MatchResult>() {
        // Use a matcher internally.
        final Matcher matcher = p.matcher(input);
        // Keep a match around that supports any interleaving of hasNext/next calls.
        MatchResult pending;

        public boolean hasNext() {
          // Lazily fill pending, and avoid calling find() multiple times if the
          // clients call hasNext() repeatedly before sampling via next().
          if (pending == null && matcher.find()) {
            pending = matcher.toMatchResult();
          }
          return pending != null;
        }

        public MatchResult next() {
          // Fill pending if necessary (as when clients call next() without
          // checking hasNext()), throw if not possible.
          if (!hasNext()) { throw new NoSuchElementException(); }
          // Consume pending so next call to hasNext() does a find().
          MatchResult next = pending;
          pending = null;
          return next;
        }

        /** Required to satisfy the interface, but unsupported. */
        public void remove() { throw new UnsupportedOperationException(); }
      };
    }
  };
}