Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use regex to get all the characters after a specific character, e.g. comma (",")

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

like image 503
user357034 Avatar asked Oct 30 '10 13:10

user357034


People also ask

How do you match a comma in regex?

The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ; . The closing ] indicates the end of the character set. The plus + indicates that one or more of the "previous item" must be present.

What does \b do in regular expression?

Simply put: \b allows you to perform a “whole words only” search using a regular expression in the form of \bword\b. A “word character” is a character that can be used to form words. All characters that are not “word characters” are “non-word characters”.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


2 Answers

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 // 
like image 111
HoLyVieR Avatar answered Sep 28 '22 05:09

HoLyVieR


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.

like image 40
PleaseStand Avatar answered Sep 28 '22 06:09

PleaseStand