Reputation: 6163
Can somebody help me construct this regular expression, please...
Given the following strings...
I need a regular expression that will extract the two numbers from the text. The month name will vary. The brackets, "widgets less" and "sprockets" text are not expected to change between strings, however, it would be really useful if this text was able to be varied as well.
Upvotes: 40
Views: 225044
Reputation: 2385
Here is the C# code to get the two numbers from a string:
Regex regex = new Regex(@"[\d]+");
var matchCollection = regex.Matches(text);
int firstNumber = int.Parse(matchCollection[0].Value);
int secondNumber = int.Parse(matchCollection[1].Value);
Upvotes: 1
Reputation: 46
On bigquery you need to make sure to use the 'r' preceding the expression:
REGEXP_EXTRACT(my_string,r'\d+')
This will extract all digits from a string column.
Upvotes: 0
Reputation: 27536
you could use something like:
[^0-9]+([0-9]+)[^0-9]+([0-9]+).+
Then get the first and second capture groups.
Upvotes: 3
Reputation: 6776
if you know for sure that there are only going to be 2 places where you have a list of digits in your string and that is the only thing you are going to pull out then you should be able to simply use
\d+
Upvotes: 76
Reputation: 336478
^\s*(\w+)\s*\(\s*(\d+)\D+(\d+)\D+\)\s*$
should work. After the match, backreference 1 will contain the month, backreference 2 will contain the first number and backreference 3 the second number.
Explanation:
^ # start of string
\s* # optional whitespace
(\w+) # one or more alphanumeric characters, capture the match
\s* # optional whitespace
\( # a (
\s* # optional whitespace
(\d+) # a number, capture the match
\D+ # one or more non-digits
(\d+) # a number, capture the match
\D+ # one or more non-digits
\) # a )
\s* # optional whitespace
$ # end of string
Upvotes: 39