“不匹配空白字符”的正则表达式是:\S

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

What is the Regular Expression For "Not Whitespace"

问题

正则表达式中使用的是这个:

/name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*[A-Za-z0-9])(?!\S)/g;

我想要的输入示例是:

  1. 名称 测试
  2. 名称 测试-名称
  3. 名称 测试-名称-2

但是当我尝试检查这个输入时,它也允许:

  1. 名称 测试-名称 任何文本

我想要的是在 测试-名称 之后不应允许任何空格或字符。

英文:

Since i try to used this regular expression

/name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*[A-Za-z0-9])(?!\S)/g;

my input that i want is for example:

  1. name test
  2. name test-name
  3. name test-name-2

but when i try to check this input it's also allowed

  1. name test-name anytext

what i want is after test-name shouldn't allowed any whitespaces or characters anymore

答案1

得分: 1

根据我的了解,这是 \S(大写的S)。因此,您可以尝试这样写:

regex = /name\s\S+$/gm

维基百科正则表达式文章

英文:

To my knowledge it is \S (uppercase s). So you can try this:

regex = /name\s\S+$/gm

WIKIPEDIA Article for Regular Expressions

答案2

得分: 0

你可以尝试这样的代码部分:
name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*[A-Za-z0-9])(?!\S)(?! )

英文:

Hi maybe try something like
name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*[A-Za-z0-9])(?!\S)(?! )

I added (?! ) so it means that a space is not allowed after your string

“不匹配空白字符”的正则表达式是:\S

答案3

得分: 0

正则表达式用于匹配非空白字符(\s)的相反部分是 \S

public class main {
    public static void main(String[] args) {
        String name = "Stack Overflow is cool";
        // 用 * 替换非空白字符
        String result = name.replaceAll("\\S", "*");
        System.out.println(result);
    }
}

这个程序的输出是 "***** ******** ** ****"。请注意,在Java中,反斜杠必须进行转义。

英文:

The regular expression for the opposite of whitespace (\s) is \S:

public class main {
        public static void main(String[] args) {
            String name = "Stack Overflow is cool";
            //replace non whitespace with *
            String result = name.replaceAll("\\S", "*");
            System.out.println(result);
        }
}

The output of this is "***** ******** ** ****". Note that in Java, backspaces have to be escaped.

答案4

得分: 0

请尝试使用以下正则表达式代替:

/^name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*[A-Za-z0-9])(?:-[\d]+)?$/

我已经使用以下测试案例验证了这个模式:

const regex = /^name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*[A-Za-z0-9])(?:-[\d]+)?/;

const input1 = "name test";
const input2 = "name test-name";
const input3 = "name test-name-2";
const input4 = "test-name ";

console.log(regex.test(input1));  // 输出: true
console.log(regex.test(input2));  // 输出: true
console.log(regex.test(input3));  // 输出: true
console.log(regex.test(input4));  // 输出: false

希望这对您有所帮助!

英文:

Try this instead:

/^name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*[A-Za-z0-9])(?:-[\d]+)?$/

I have checked the pattern with this test case.

const regex = /^name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*[A-Za-z0-9])(?:-[\d]+)?$/;

const input1 = "name test";
const input2 = "name test-name";
const input3 = "name test-name-2";
const input4 = "test-name ";

console.log(regex.test(input1));  // Output: true
console.log(regex.test(input2));  // Output: true
console.log(regex.test(input3));  // Output: true
console.log(regex.test(input4));  // Output: false

I hope it works for you!

答案5

得分: 0

试试这个!

name\s{1}+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*[A-Za-z0-9])*(?:-+[A-Za-z0-9]+)(?!\S)(?! )

希望对你有帮助!

英文:

> Try this!

name\s{1}+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*[A-Za-z0-9])*(?:-+[A-Za-z0-9]+)(?!\S)(?! )

“不匹配空白字符”的正则表达式是:\S
I hope it works for you!

答案6

得分: 0

可以在正则表达式中使用行尾符号 $ 来使其工作。

修改后的正则表达式 :

/name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*)$/

正则表达式解释 :

  • / - 正则表达式模式的开始和结束斜杠。
  • name - 精确匹配字符串 "name"。
  • \s+ - 匹配一个或多个空白字符(空格、制表符等)。
  • ( - 开始捕获组。该组的内容将被提取为结果。
  • [A-Za-z0-9]+ - 匹配一个或多个字母或数字字符。
  • (?:-[A-Za-z0-9]+)* - 这是一个非捕获组 (?: ... ),后面跟着 *,表示它可以匹配零个或多个出现在内部的模式。此处的模式匹配连字符后面跟着一个或多个字母或数字字符。
  • ) - 结束捕获组。
  • $ - 将模式锚定到输入的结尾。这确保了匹配必须发生在输入的结尾。

工作示例 :

// 输入字符串示例
const inputs = [
  'name test',
  'name test-name',
  'name test-name-2',
  'name test-name anytext'
];

// 正则表达式模式
const regex = /^name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*)$/;

// 遍历输入
inputs.forEach((input) => {
  const isMatch = regex.test(input);

  console.log(`${input} : ${isMatch}`);
});
英文:

You can use end-of-line symbol $ in your regular expression to make it work.

Modified regular expression :

/name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*)$/

RegEx explanation :

/ - The forward slashes at the beginning and end delimit the regular expression pattern.<br>
name - Matches the literal string "name" exactly as it appears.<br>
\s+ - Matches one or more whitespace characters (spaces, tabs, etc.).<br>
( - Begins a capturing group. The contents of this group will be extracted as a result.<br>
[A-Za-z0-9]+ - Matches one or more alphanumeric characters (letters or digits).<br>
(?:-[A-Za-z0-9]+)* - This is a non-capturing group (?: ... ) followed by *, which means it can match zero or more occurrences of the pattern inside. The pattern here matches a hyphen followed by one or more alphanumeric characters.<br>
) - Ends the capturing group.<br>
$ - Anchors the pattern to the end of the string. This ensures that the match must occur at the end of the input.

Working Demo :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Input string examples
const inputs = [
  &#39;name test&#39;,
  &#39;name test-name&#39;,
  &#39;name test-name-2&#39;,
  &#39;name test-name anytext&#39;
];

// Regular expression pattern
const regex = /^name\s+([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*)$/;

// Iterate over the inputs
inputs.forEach((input) =&gt; {
  const isMatch = regex.test(input);

  console.log(`${input} : ${isMatch}`);
});

<!-- end snippet -->

答案7

得分: 0

我会选择
```name \S+```

或者,如果你想要由连字符分割的字符串

name \w+(?:-\w+)*$

英文:

I'd go with
name \S+

Or, if you want strings split by hyphen

name \w+(?:-\w+)*$

huangapple
  • 本文由 发表于 2023年7月17日 14:19:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701918.html
匿名

发表评论

匿名网友

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

确定