简体   繁体   English

正则表达式 - 允许空格和任意位数

[英]Regular expression - allow space and any number of digits

my valid string should be either "1234" or " 1234"我的有效字符串应该是"1234" or " 1234"

allow one or zero space at the beginning在开头允许one or zero space

then followed by any number of digits only然后仅后跟任意数量的digits

so what should be the regular expression for this?那么这个正则表达式应该是什么?

You can use this:你可以使用这个:

^ ?\d+$

which is easier to read like this:像这样更容易阅读:

^[ ]?\d+$

See demo .演示

To test if you have a match, you can do (for instance):要测试是否匹配,您可以执行(例如):

if (subjectString.matches("[ ]?\\d+")) {
    // It matched!
    } 
else {  // nah, it didn't match...  } 

Here you don't need the ^ and $ anchors, because the matches method looks for an exact match.这里不需要^$锚点,因为matches方法会查找完全匹配。

Explanation解释

  • The ^ anchor asserts that we are at the beginning of the string. ^锚断言我们在字符串的开头。 Depending on the method used, these anchors may not be needed.根据使用的方法,可能不需要这些锚。
  • [ ]? matches zero or one space.匹配零个或一个空格。 The brackets are not needed, but they make it easier to read.括号不是必需的,但它们更易于阅读。 You can remove them.您可以删除它们。 Do not use \s there as it also matches newlines and tabs.不要在那里使用\s因为它也匹配换行符和制表符。
  • \d+ matches one or more digits \d+匹配一位或多位数字
  • The $ anchor asserts that we are at the end of the string $锚断言我们在字符串的末尾

It should be \\s?\\d+应该是\\s?\\d+

System.out.println(Pattern.matches("\\s?\\d+", "1234"));  // true
System.out.println(Pattern.matches("\\s?\\d+", " 1234"));  // true
System.out.println(Pattern.matches("\\s?\\d+", "  1234"));  // false

\s denotes whitespaces and the ? \s表示空格,而? means zero or one occurence.表示零次或一次发生。 The \d corresponds to a digit with + meaning at least one occurence. \d对应一个数字, +表示至少出现一次。

In case only the space character is allowed (eg no tabs), use ( )?\\d+如果只允许使用空格字符(例如没有制表符),请使用( )?\\d+

Your regex would be,你的正则表达式是,

^\s?[0-9]+$

Demo演示

Explanation:解释:

  • ^ Asserts that we are at the begining of the line. ^断言我们在行首。
  • \s? A zero or one space is allowed.允许有 0 或 1 个空格。
  • [0-9]+ One or more numbers. [0-9]+一个或多个数字。
  • $ Asserts that we are at the end of the line. $断言我们在行尾。

If your input data must be only one space (not a whitespace), so the regex you need is ?\d+ (Note the space before "?").如果您的输入数据必须只有一个空格(不是空格),那么您需要的正则表达式是?\d+ (注意“?”之前的空格)。

On the other hand, if your input data must contain a whitespace, so you need to tweak the regex to:另一方面,如果您的输入数据必须包含空格,那么您需要将正则表达式调整为:

\s?\d+

A whitespace character can be:空白字符可以是:

A space character
A tab character
A carriage return character
A new line character
A vertical tab character
A form feed character

As a note, if you need to discard all any character after your digits, for instace 1234fff doesn't have to be matched, then you can fix your regex to: \s?\d+\b (this will make your regex to have a boundary).请注意,如果您需要丢弃数字之后的所有字符,因为不必匹配实例1234fff ,那么您可以将您的正则表达式修复为: \s?\d+\b (这将使您的正则表达式具有边界)。

Remember to escape backslashes in java code, so \s?\d+ will be \\s?\\d+记得在 java 代码中转义反斜杠,所以\s?\d+将是\\s?\\d+

Below you can find the regex for one space followed by only digits.您可以在下面找到一个空格的正则表达式,后跟只有数字。

在此处输入图像描述

try this: "[\\d\\s]+"试试这个: “[\\d\\s]+”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM