tags:

views:

85

answers:

4

Hi, I have strings like follows:

val:key

I can capture 'val' with /^\w*/.

How can I now get 'key' without the ':' sign?

Thanks

+3  A: 

How about this?

/^(\w+):(\w+)$/

Or if you just want to capture everything after the colon:

/:(.+)/

Here's a less clear example using a lookbehind assertion to ensure a colon occurred before the match - the entire match will not include that colon.

/(?<=:).*/
Paul Dixon
i'd like to capture it with a different regex and /\:(.*)/ still captures the ':'
pistacchio
but the parentheses will let you extract the submatch
Paul Dixon
A: 
/\:(\w*)/

That looks for : and then captures all the word characters after it till the end of the string

AutomatedTester
+1  A: 

What language are you using? /\:(.*)/ doesn't capture the ":" but it does match the ':'

In Perl, if you say:

$text =~ /\:(.*)/;
$capture = $1;
$match = $&;

Then $capture won't have the ":" and $match will. (But try to avoid using $& as it slows down Perl: this was just to illustrate the match).

Adrian Pronk
+1  A: 

This will capture the key in group 1 and the value in group 2. It should work correctly even when the value contails a colon (:) character.

^(\w+?):(.*)
Reuben Peeris