如何使用正则表达式确定一个数字是否为二进制,并且具有偶数个零?

huangapple go评论68阅读模式
英文:

How to determine if a number is binary and has an even amount of zeros with only one regex?

问题

我想要一个正则表达式来判断一个数字是否为二进制,并且其中的零数量为偶数。

例如,如果我们考虑这三个数字:

  • 10001010
  • 00010010
  • 45671892

我希望正则表达式匹配以下内容:

  • 10001010 // 由于零的数量不均匀,所以不匹配
  • 00010010 // 因为零的数量为偶数,所以匹配
  • 45671892 // 不匹配,因为它不是二进制数

目前的解决方案如下:

[1]*(0[1]*0[1]*)*

我已经在 https://regex101.com/ 上进行了测试,对我来说它是有效的。
问题仍然在线上,只是为了看是否有人能提出更有效的解决方案。

非常感谢每一次的帮助。

提前谢谢!

英文:

I want a regex to determine if a number is binary and if it has an even amount of zeros in it.

For example, if we consider these 3 numbers:

  • 10001010
  • 00010010
  • 45671892

I want the regex to match the following:

  • 10001010 //Does NOT match because it has an uneven amount of zeros
  • 00010010 //Does match because it has an even amount of zeros
  • 45671892 //Does not match because it is not a binary number

The current solution is the following:

[1]*(0[1]*0[1]*)*

I tested it with https://regex101.com/ and it works for me.
The question is still online just to see if anyone can come up with a more efficient solution

I am thankful for every help.

Thank you in advance!

答案1

得分: 3

^[1]*(0[1]*0[1]*)*$
这个正则表达式可以这样解释:

查找任意数量的`1`,后面跟随一对`00`,中间可以有零个或多个`1`。
英文:
^[1]*(0[1]*0[1]*)*$

which can be interpreted in the following way:

find any number of 1 followed by pairs of 00 with optional one or more 1s in between.

答案2

得分: 2

你可以使用 Long#parseLong 方法来检查给定的数字字符串是否表示二进制数字。如果是的话,你可以从数字字符串中获得一个流,筛选出 0,计算 0 的数量,并检查数量是否能被 2 整除,从而确定有奇数个还是偶数个 0

public class Main {
    public static void main(String[] args) {
        String[] arr = { "10001010", "00010010", "45671892" };

        for (String s : arr) {
            // 检查数字字符串是否为二进制
            try {
                // 可以验证最多 64 位
                Long.parseLong(s, 2);
                if (s.chars().filter(c -> c == '0').count() % 2 == 0) {
                    System.out.println(s + " 表示二进制数字且有偶数个 0。");
                } else {
                    System.out.println(s + " 表示二进制数字且有奇数个 0。");
                }
            } catch (Exception e) {
                System.out.println(s + " 不表示二进制数字。");
            }
        }
    }
}

输出结果:

10001010 表示二进制数字且有奇数个 0。
00010010 表示二进制数字且有偶数个 0。
45671892 不表示二进制数字。
英文:

You can use Long#parseLong to check if the given number string represents a binary number or not. If yes, you can get a stream out of the number string, filter for 0, count the number of 0s and check if the count is divisible by 2 or not to determine if it has odd or even number of 0s.

public class Main {
	public static void main(String[] args) {
		String[] arr = { "10001010", "00010010", "45671892" };

		for (String s : arr) {
			// Check if the number string is binary
			try {
                // Can validate up to 64 bits
				Long.parseLong(s, 2);
				if (s.chars().filter(c -> c == '0').count() % 2 == 0) {
					System.out.println(s + " represents a binary number and has an even number of 0s.");
				} else {
					System.out.println(s + " represents a binary number and has an odd number of 0s.");
				}
			} catch (Exception e) {
				System.out.println(s + " does not represent a binary number.");
			}
		}
	}
}

Output:

10001010 represents a binary number and has an odd number of 0s.
00010010 represents a binary number and has an even number of 0s.
45671892 does not represent a binary number.

huangapple
  • 本文由 发表于 2020年10月6日 19:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64225281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定