开发者

Regular Expression To Match a Text Inside : -

I'm building a project that needs to match a text that is inside : text to match -. For example, if I have this string:

nathanpc/ltweet: Asking a question at #StackOverflow: "Regular Expr开发者_如何学Cession To Match a Text Inside : -" - @devs

I want to match Asking a question at #StackOverflow: "Regular Expression To Match a Text Inside : -". Everything that is after the first : and the last -, but what is the correct regular expression to do this?

PS: I'm using Javascript with jQuery


If you're using Perl-compatible regular expressions, like those found in most languages:

/:(.*)-/

You might not need the slashes, depending on the language. The capture group 1 will get the content you want.

The .* is a greedy matcher, so it will attempt to match as many characters as possible, up to the last dash in the input.


If you use a greedy operator like . it will try to form the largest match possible (at least in Perl compatible regular expression engines)

So to match this something as simple as :(.*)- will work.

See this example script in Perl:

my $str = "Discard:Testing:- one two three -discard";
$str =~ m/:(.*)-/;
print $1;

$1 = "Testing:- one two three"

Or in javascript here: http://www.regular-expressions.info/javascriptexample.html


This works for me (ruby):

[^:]+:\s+([^-]+.*?)\s-

Test:

 test = "nathanpc/ltweet: Asking a question at #StackOverflow: 'Regular Expression To Match a Text Inside : -' - @devs"

 m = test.match /[^:]+:\s+([^-]+.*?)\s-/

then

 m[1].to_s

produces

Asking a question at #StackOverflow: 'Regular Expression To Match a Text Inside : -'

As others have noticed the much simpler :(.*)- works the same. Perhaps without spaces :\s(.*)\s-


"Standard" regular expressions cannot parse this text exactly as you described. That parsing requires some context, that cannot be expressed with regexes. For example, when receiving the first "-", how does the expression not to end?

Out of my head, I can only come with Perl extended regular expressions, that allow sub-expression parsing, but it won't be easy, as you have to count the occurrences of ":" and "-" to match exactly the last one.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜