如何使用regex获取特定字符后的所有字符,例如逗号(“,”)

时间:2022-08-22 13:16:50

Need a Regex to get all characters after , (not including it) from a variable. This variable can contain for example

需要一个正则表达式来从变量中获取所有字符(不包括它)。这个变量可以包含例如。

'SELECT___100E___7',24
'SELECT___100E___7',1
'SELECT___100E___7',286
'SELECT___100E___7',5147

Note: There can be any length of characters after the , in this variable.

注意:在这个变量中,在后面可以有任意长度的字符。

An explanation of the regexp would be a added help for the novice :)

对regexp的解释将对新手有帮助:)

Edit: a javascript answer would be just as good

编辑:javascript的答案也一样好

9 个解决方案

#1


99  

You don't need regex to do this. Here's an example :

你不需要regex来做这个。这里有一个例子:

var str = "'SELECT___100E___7',24";
var afterComma = str.substr(str.indexOf(",") + 1); // Contains 24 //

#2


29  

Short answer

Either:

:

  • ,[\s\S]*$ or ,.*$ to match everything after the first comma (see explanation for which one to use); or

    ,[\ s \ s]*或美元。*$匹配第一个逗号后的所有内容(请参阅使用哪个逗号的解释);或

  • [^,]*$ to match everything after the last comma (which is probably what you want).

    [^]* $匹配一切在最后一个逗号(这可能是你想要的)。

You can use, for example, /[^,]*/.exec(s)[0] in JavaScript, where s is the original string. If you wanted to use multiline mode and find all matches that way, you could use s.match(/[^,]*/mg) to get an array (if you have more than one of your posted example lines in the variable on separate lines).

例如,您可以使用/[^,]* / .exec(s)[0]在JavaScript中,s是原始字符串。如果你想使用多行模式,找到所有匹配,您可以使用s.match(/[^,]* /毫克)得到一个数组(如果你有一个以上的例子在变量在不同的线行)。

Explanation

  • [\s\S] is a character class that matches both whitespace and non-whitespace characters (i.e. all of them). This is different from . in that it matches newlines.
  • [\s\ s\ s\ s]是一个同时匹配空格和非空格字符(即所有字符)的字符类。这是不同的。它匹配新行。
  • `[^,] is a negated character class that matches everything except for commas.
  • [^],是一个否定相匹配的字符类除了逗号。
  • * means that the previous item can repeat 0 or more times.
  • *表示前一项可以重复0次或多次。
  • $ is the anchor that requires that the end of the match be at the end of the string (or end of line if using the /m multiline flag).
  • $是要求匹配结束在字符串末尾的锚(如果使用/m multiline标志,则是行结束)。

For the first match, the first regex finds the first comma , and then matches all characters afterward until the end of line [\s\S]*$, including commas.

对于第一个匹配,第一个regex找到第一个逗号,然后匹配所有字符,直到行结束[\s\ s\ s\ s]*$,包括逗号。

The second regex matches as many non-comma characters as possible before the end of line. Thus, the entire match will be after the last comma.

第二个regex在行结束前匹配尽可能多的非逗号字符。因此,整个匹配将位于最后一个逗号之后。

#3


14  

[^,]*$

might do. (Matches everything after the last comma).

可能做的。(在最后一个逗号之后匹配所有内容)。

Explanation: [^,] matches every character except for ,. The * denotes that the regexp matches any number of repetition of [^,]. The $ sign matches the end of the line.

解释:[^]匹配每个角色除了,。*表示的regexp匹配任意数量的重复[^,]。$符号匹配行尾。

#4


3  

This matches a word from any length:

这与任何长度的单词都匹配:

var phrase = "an important number comes after this: 123456";
var word = "this: ";
var number = phrase.substr(phrase.indexOf(word) + word.length);
// number = 123456

#5


2  

.+,(.+)

Explanation:

解释:

.+,

will search for everything before the comma, including the comma.

将在逗号之前搜索所有内容,包括逗号。

(.+) 

will search for everything after the comma, and depending on your regex environment,

将在逗号之后搜索所有内容,根据您的regex环境,

\1

is the reference for the first parentheses captured group that you need, in this example, everything after the comma.

是您需要的第一个圆括号捕获组的引用,在本例中,在逗号之后的所有内容。

#6


1  

You can try with the following:

你可以试试下面的方法:

new_string = your_string.split(',').pop().trim();

This is how it works:

这就是它的工作原理:

  • split(',') creates an array made of the different parts of your_string
  • split(',')创建一个由your_string的不同部分组成的数组

(e.g. if the string is "'SELECT___100E___7',24", the array would be ["'SELECT___100E___7'", "24"]).

(例如,如果字符串是“SELECT___100E___7,24”,数组将[“SELECT___100E___7”、“24”))。

  • pop() gets the last element of the array
  • pop()获取数组的最后一个元素

(in the example, it would be "24").

(在本例中,应该是“24”)。

This would already be enough, but in case there might be some spaces (not in the case of the OP, but more in general), we could have:

这已经足够了,但如果可能有一些空间(不是OP的情况,而是更一般的情况),我们可以:

  • trim() that would remove the spaces around the string (in case it would be " 24 ", it would become simply "24")
  • trim()将会移除字符串周围的空间(如果它是“24”,它会变成简单的“24”)

It's a simple solution and surely easier than a regexp.

这是一个简单的解决方案,肯定比regexp简单。

#7


1  

Maybe you can try this

也许你可以试试这个

var str = "'SELECT___100E___7',24";
    var res = str.split(',').pop();

#8


0  

This should work

这应该工作

preg_match_all('@.*\,(.*)@', '{{your data}}', $arr, PREG_PATTERN_ORDER);

You can test it here: http://www.spaweditor.com/scripts/regex/index.php

您可以在这里测试:http://www.spaweditor.com/scripts/regex/index.php。

RegEx: .*\,(.*)

RegEx:. * \(. *)

Same RegEx test here for JavaScript: http://www.regular-expressions.info/javascriptexample.html

同样的RegEx JavaScript测试:http://www.regular-expressions.info/javascriptexample.html

#9


0  

Another idea is to do myVar.split(',')[1];

另一个想法是做myVar.split(',')[1];

For simple case, not using a regexp is a good idea...

对于简单的情况,不使用regexp是一个好主意……

#1


99  

You don't need regex to do this. Here's an example :

你不需要regex来做这个。这里有一个例子:

var str = "'SELECT___100E___7',24";
var afterComma = str.substr(str.indexOf(",") + 1); // Contains 24 //

#2


29  

Short answer

Either:

:

  • ,[\s\S]*$ or ,.*$ to match everything after the first comma (see explanation for which one to use); or

    ,[\ s \ s]*或美元。*$匹配第一个逗号后的所有内容(请参阅使用哪个逗号的解释);或

  • [^,]*$ to match everything after the last comma (which is probably what you want).

    [^]* $匹配一切在最后一个逗号(这可能是你想要的)。

You can use, for example, /[^,]*/.exec(s)[0] in JavaScript, where s is the original string. If you wanted to use multiline mode and find all matches that way, you could use s.match(/[^,]*/mg) to get an array (if you have more than one of your posted example lines in the variable on separate lines).

例如,您可以使用/[^,]* / .exec(s)[0]在JavaScript中,s是原始字符串。如果你想使用多行模式,找到所有匹配,您可以使用s.match(/[^,]* /毫克)得到一个数组(如果你有一个以上的例子在变量在不同的线行)。

Explanation

  • [\s\S] is a character class that matches both whitespace and non-whitespace characters (i.e. all of them). This is different from . in that it matches newlines.
  • [\s\ s\ s\ s]是一个同时匹配空格和非空格字符(即所有字符)的字符类。这是不同的。它匹配新行。
  • `[^,] is a negated character class that matches everything except for commas.
  • [^],是一个否定相匹配的字符类除了逗号。
  • * means that the previous item can repeat 0 or more times.
  • *表示前一项可以重复0次或多次。
  • $ is the anchor that requires that the end of the match be at the end of the string (or end of line if using the /m multiline flag).
  • $是要求匹配结束在字符串末尾的锚(如果使用/m multiline标志,则是行结束)。

For the first match, the first regex finds the first comma , and then matches all characters afterward until the end of line [\s\S]*$, including commas.

对于第一个匹配,第一个regex找到第一个逗号,然后匹配所有字符,直到行结束[\s\ s\ s\ s]*$,包括逗号。

The second regex matches as many non-comma characters as possible before the end of line. Thus, the entire match will be after the last comma.

第二个regex在行结束前匹配尽可能多的非逗号字符。因此,整个匹配将位于最后一个逗号之后。

#3


14  

[^,]*$

might do. (Matches everything after the last comma).

可能做的。(在最后一个逗号之后匹配所有内容)。

Explanation: [^,] matches every character except for ,. The * denotes that the regexp matches any number of repetition of [^,]. The $ sign matches the end of the line.

解释:[^]匹配每个角色除了,。*表示的regexp匹配任意数量的重复[^,]。$符号匹配行尾。

#4


3  

This matches a word from any length:

这与任何长度的单词都匹配:

var phrase = "an important number comes after this: 123456";
var word = "this: ";
var number = phrase.substr(phrase.indexOf(word) + word.length);
// number = 123456

#5


2  

.+,(.+)

Explanation:

解释:

.+,

will search for everything before the comma, including the comma.

将在逗号之前搜索所有内容,包括逗号。

(.+) 

will search for everything after the comma, and depending on your regex environment,

将在逗号之后搜索所有内容,根据您的regex环境,

\1

is the reference for the first parentheses captured group that you need, in this example, everything after the comma.

是您需要的第一个圆括号捕获组的引用,在本例中,在逗号之后的所有内容。

#6


1  

You can try with the following:

你可以试试下面的方法:

new_string = your_string.split(',').pop().trim();

This is how it works:

这就是它的工作原理:

  • split(',') creates an array made of the different parts of your_string
  • split(',')创建一个由your_string的不同部分组成的数组

(e.g. if the string is "'SELECT___100E___7',24", the array would be ["'SELECT___100E___7'", "24"]).

(例如,如果字符串是“SELECT___100E___7,24”,数组将[“SELECT___100E___7”、“24”))。

  • pop() gets the last element of the array
  • pop()获取数组的最后一个元素

(in the example, it would be "24").

(在本例中,应该是“24”)。

This would already be enough, but in case there might be some spaces (not in the case of the OP, but more in general), we could have:

这已经足够了,但如果可能有一些空间(不是OP的情况,而是更一般的情况),我们可以:

  • trim() that would remove the spaces around the string (in case it would be " 24 ", it would become simply "24")
  • trim()将会移除字符串周围的空间(如果它是“24”,它会变成简单的“24”)

It's a simple solution and surely easier than a regexp.

这是一个简单的解决方案,肯定比regexp简单。

#7


1  

Maybe you can try this

也许你可以试试这个

var str = "'SELECT___100E___7',24";
    var res = str.split(',').pop();

#8


0  

This should work

这应该工作

preg_match_all('@.*\,(.*)@', '{{your data}}', $arr, PREG_PATTERN_ORDER);

You can test it here: http://www.spaweditor.com/scripts/regex/index.php

您可以在这里测试:http://www.spaweditor.com/scripts/regex/index.php。

RegEx: .*\,(.*)

RegEx:. * \(. *)

Same RegEx test here for JavaScript: http://www.regular-expressions.info/javascriptexample.html

同样的RegEx JavaScript测试:http://www.regular-expressions.info/javascriptexample.html

#9


0  

Another idea is to do myVar.split(',')[1];

另一个想法是做myVar.split(',')[1];

For simple case, not using a regexp is a good idea...

对于简单的情况,不使用regexp是一个好主意……