将一个组放入一个组[123 [a-u]]

时间:2022-09-16 00:39:09

I'm having a lot more difficulty than I anticipated in creating a simple regex to match any specific characters, including a range of characters from the alphabet.

我创建一个简单的正则表达式以匹配任何特定字符(包括字母表中的一系列字符)时,我遇到的困难比我预期的要困难得多。

I've been playing with regex101 for a while now, but every combination seems to result in no matches.

我已经玩regex101一段时间了,但是每个组合似乎都没有匹配。

Example expression: [\n\r\t\s\(\)-]

示例表达式:[\ n \ r \ t \ s \(\) - ]

Preferred expression: [[a-z][a-Z]\n\r\t\s\(\)-]

首选表达式:[[a-z] [a-Z] \ n \ r \ t \ t \ s \(\) - ]

Example input: (123) 241()-127()()() abc ((((((((

输入示例:(123)241() - 127()()()abc((((((((

Ideally the expression will capture every character except the digits

理想情况下,表达式将捕获除数字之外的每个字符

I know I could always manually input "abcdefgh".... but there has to be an easier way. I also know there are easier ways to capture numbers only, but there are some special characters and letters which I may eventually need to include as well.

我知道我总是可以手动输入“abcdefgh”....但必须有一个更简单的方法。我也知道有更简单的方法来捕获数字,但是我可能最终还需要包含一些特殊的字符和字母。

3 个解决方案

#1


1  

With regex you can set the regex expression to trigger on a range of characters like in your above example [a-z] that will capture any letter in the alphabet that is between a and z. To trigger on more than one character you can add a "+" to it or, if you want to limit the number of characters captured you can use {n} where n is the number of characters you want to capture. So, [a-z]+ is one or more and [a-z]{4} would match on the first four characters between a and z.

使用正则表达式,您可以将正则表达式设置为触发一系列字符,例如上面的示例[a-z],它将捕获字母表中a和z之间的任何字母。要触发多个字符,可以向其添加“+”,或者,如果要限制捕获的字符数,可以使用{n},其中n是要捕获的字符数。因此,[a-z] +是一个或多个,[a-z] {4}将匹配a和z之间的前四个字符。

#2


1  

You can use partial intervals. For example, [a-j] will match all characters from a to j. So, [a-j]{2} for string a6b7cd will match only cd. Also you can use these intervals several times within same group like this: [a-j4-6]{4}. This regex will match ab44 but not ab47

您可以使用部分间隔。例如,[a-j]将匹配a到j中的所有字符。因此,字符串a6b7cd的[a-j] {2}将仅匹配cd。您也可以在同一组中多次使用这些间隔:[a-j4-6] {4}。这个正则表达式将匹配ab44而不是ab47

#3


0  

Overlooked a pretty small character. The term I was looking for was "Alternative" apparently.

忽略了一个非常小的角色。我正在寻找的术语显然是“替代”。

[\r\t\n]|[a-z] with the missing element being the | character. This will allow it to match anything from the first group, and then continue on to match the second group.

[\ r \ t \ n] | [a-z]缺少元素为|字符。这将允许它匹配第一组中的任何内容,然后继续匹配第二组。

At least that's my conclusion when testing this specific example.

在测试这个具体的例子时,至少这是我的结论。

#1


1  

With regex you can set the regex expression to trigger on a range of characters like in your above example [a-z] that will capture any letter in the alphabet that is between a and z. To trigger on more than one character you can add a "+" to it or, if you want to limit the number of characters captured you can use {n} where n is the number of characters you want to capture. So, [a-z]+ is one or more and [a-z]{4} would match on the first four characters between a and z.

使用正则表达式,您可以将正则表达式设置为触发一系列字符,例如上面的示例[a-z],它将捕获字母表中a和z之间的任何字母。要触发多个字符,可以向其添加“+”,或者,如果要限制捕获的字符数,可以使用{n},其中n是要捕获的字符数。因此,[a-z] +是一个或多个,[a-z] {4}将匹配a和z之间的前四个字符。

#2


1  

You can use partial intervals. For example, [a-j] will match all characters from a to j. So, [a-j]{2} for string a6b7cd will match only cd. Also you can use these intervals several times within same group like this: [a-j4-6]{4}. This regex will match ab44 but not ab47

您可以使用部分间隔。例如,[a-j]将匹配a到j中的所有字符。因此,字符串a6b7cd的[a-j] {2}将仅匹配cd。您也可以在同一组中多次使用这些间隔:[a-j4-6] {4}。这个正则表达式将匹配ab44而不是ab47

#3


0  

Overlooked a pretty small character. The term I was looking for was "Alternative" apparently.

忽略了一个非常小的角色。我正在寻找的术语显然是“替代”。

[\r\t\n]|[a-z] with the missing element being the | character. This will allow it to match anything from the first group, and then continue on to match the second group.

[\ r \ t \ n] | [a-z]缺少元素为|字符。这将允许它匹配第一组中的任何内容,然后继续匹配第二组。

At least that's my conclusion when testing this specific example.

在测试这个具体的例子时,至少这是我的结论。