java使用regex将字符串解析为变量

zf2sa74q  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(407)

我需要从字符串中提取变量。

  1. String format = "x:y";
  2. String string = "Marty:McFly";

然后

  1. String x = "Marty";
  2. String y = "McFly";

但是格式可以是任何东西,它可以看起来像这个y?x=>mcfly?marty
如何使用regex解决这个问题?
编辑:当前解决方案

  1. String delimiter = format.replace(Y, "");
  2. delimiter = delimiter.replaceAll(X, "");
  3. delimiter = "\\"+delimiter;
  4. String strings[] = string.split(delimiter);
  5. String x;
  6. String y;
  7. if(format.startsWith(X)){
  8. x = strings[0];
  9. y = strings[1];
  10. }else{
  11. y = strings[0];
  12. x = strings[1];
  13. }
  14. System.out.println(x);
  15. System.out.println(y);

这很管用,但我更喜欢干净的溶液。

nom7f22z

nom7f22z1#

您可以使用以下正则表达式 (\\w)(\\W)(\\w) 这将发现任何字母数字字符后跟任何非字母数字字符后跟另一组字母数字字符。括号将对查找进行分组,因此组1将是参数1,组2将是分隔符,组3将是参数2。
比较参数1和参数2可以确定它们的词序。
样品

  1. public static void main(String[] args) {
  2. testString("x:y", "Marty:McFly");
  3. testString("x?y", "Marty?McFly");
  4. testString("y:x", "Marty:McFly");
  5. testString("y?x", "Marty?McFly");
  6. }
  7. /**
  8. *
  9. */
  10. private static void testString(String format, String string) {
  11. String regex = "(\\w)(\\W)(\\w)";
  12. Pattern pattern = Pattern.compile(regex);
  13. Matcher matcher = pattern.matcher(format);
  14. if (!matcher.find()) throw new IllegalArgumentException("no match found");
  15. String delimiter = matcher.group(2);
  16. String param1 = matcher.group(1);
  17. String param2 = matcher.group(3);
  18. String[] split = string.split("\\" + delimiter);
  19. String x;
  20. String y;
  21. switch(param1.compareTo(param2)) {
  22. case 1:
  23. x = split[1];
  24. y = split[0];
  25. break;
  26. case -1:
  27. case 0:
  28. default:
  29. x = split[0];
  30. y = split[1];
  31. };
  32. System.out.println("String x: " + x);
  33. System.out.println("String y: " + y);
  34. System.out.println(String.format("%s%s%s", x, delimiter, y));
  35. System.out.println();
  36. }

这种方法允许您使用任何类型的格式,而不仅仅是x和y。可以有任何与正则表达式匹配的格式。

展开查看全部
68bkxrlz

68bkxrlz2#

根本不需要regex。

  1. public static void main(String[] args) {
  2. test("x:y", "Marty:McFly");
  3. test("y?x", "McFly?Marty");
  4. }
  5. public static void test(String format, String input) {
  6. if (format.length() != 3 || Character.isLetterOrDigit(format.charAt(1))
  7. || (format.charAt(0) != 'x' || format.charAt(2) != 'y') &&
  8. (format.charAt(0) != 'y' || format.charAt(2) != 'x'))
  9. throw new IllegalArgumentException("Invalid format: \"" + format + "\"");
  10. int idx = input.indexOf(format.charAt(1));
  11. if (idx == -1 || input.indexOf(format.charAt(1), idx + 1) != -1)
  12. throw new IllegalArgumentException("Invalid input: \"" + input + "\"");
  13. String x, y;
  14. if (format.charAt(0) == 'x') {
  15. x = input.substring(0, idx);
  16. y = input.substring(idx + 1);
  17. } else {
  18. y = input.substring(0, idx);
  19. x = input.substring(idx + 1);
  20. }
  21. System.out.println("x = " + x);
  22. System.out.println("y = " + y);
  23. }

输出

  1. x = Marty
  2. y = McFly
  3. x = Marty
  4. y = McFly

如果格式字符串可以更改为regex,那么使用命名的捕获组将非常简单:

  1. public static void main(String[] args) {
  2. test("(?<x>.*?):(?<y>.*)", "Marty:McFly");
  3. test("(?<y>.*?)\\?(?<x>.*)", "McFly?Marty");
  4. }
  5. public static void test(String regex, String input) {
  6. Matcher m = Pattern.compile(regex).matcher(input);
  7. if (! m.matches())
  8. throw new IllegalArgumentException("Invalid input: \"" + input + "\"");
  9. String x = m.group("x");
  10. String y = m.group("y");
  11. System.out.println("x = " + x);
  12. System.out.println("y = " + y);
  13. }

与上述输出相同,包括值顺序。

展开查看全部

相关问题