开发者

regular expression: r05c75 -- extracting numbers between bookends

Today, I am learning regular expressions =D

I understand some basics, like \d+ matches any number of digits, but how would I get the values after 'r' and 'c' in the string r05c75 (the numbers are arbitrary length)

They represent row and column values and I want to extract the number between 'r' and 'c' and the number between 'c' and $ (end-of-string).

I'm hoping this question will also be elucidative for any other beginners as well.

The general case would be

aaa DDD bbb DDD ccc DDD (without spaces)

where aaa, bbb, ccc, and ddd are arbitrary (known) strings, and the D-triplets are ju开发者_Go百科st digits.

I can "match" (check the validity) of my string r05c75, but I am not sure how to extract the numbers.

This is probably a fairly common question, I'm sorry if I missed any duplicates.


(?:\w+(\d+))+ is the generic regexp that will return only match groups with the digits

The (?: ... ) syntax signifies a "non-capturing group" which allows me to say that it can repeat (with +) but without having it show up in the match groups. The other group (\d+) doesn't have the ?: and so it is captured.

So, read in English this regexp says, "match a group of one or more of one or more letters followed by one or more digits, and oh yeah capture the digit groupings".


r(\d+)c

The parentheses indicate the regular expression concept of a "capture"...if you search your ref docs, they should tell you more about that.


You can extract values by using parens:

r(\d+)c(\d+)

This will give you the row number in $1 and the column number in $2.


In general when working with regular expression you have the ability to mark a subexpression a group and after a successful match to retrieve that group contents.

In your case if we user the javascript regex engine you would write the match for r95c75 as this:

var match = "r05c75".match(/r(\d+)c(\d+)/);
if ( match ) // if successfull match
{
  var n1 = match[1];
  var n2 = match[2];
}

n1, n2 will contain the numbers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜