views:

58

answers:

7

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 :)

Edit: a javascript answer would be just as good

+1  A: 
[^,]*$

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.

Sven Marnach
This does not match everything after a `,` but anything except `,`. What if there is no `,`?
Gumbo
The examples in the original post suggested that there *is* a comma. And the behaviour without a comma was not defined, so this should be fine.
Sven Marnach
A: 

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

RegEx: .*\,(.*)

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

infinity
Sorry, it was my mistake for not including what language I wanted
You can still use the same RegEx
infinity
A: 
string pattern = ",(?<anyAfterComma>.*)\b";
match.Groups["anyAfterComma"].Value;
A_Nablsi
This is C#, I will delete it soon.
A_Nablsi
+1  A: 
.+,(.+)

Explanation:

.+,

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

(.+) 

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

\1

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

darioo
+2  A: 

Short answer

Either:

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

  • [^,]*$ 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).

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.
  • `[^,] is a negated character class that matches everything except for commas.
  • * means that the previous item can repeat 0 or more times.
  • $ 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).

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.

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.

idealmachine
you second answer worked but I decided to go with the non regex solution. Thanks for you effort!!!
+3  A: 

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

var str = "'SELECT___100E___7',24";
var afterComma = str.substr(str.indexOf(",") + 1); // Contains 24 //
HoLyVieR
Actually, no I do not absolutely need a Regex answer. I have changed my question to reflect this more clearly and your answer is quite clear and works.
A: 

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

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

romaintaz