简体   繁体   中英

Match text after colon

I want to match the word after the "type :".

What I have?

My actual pattern : (?<=type\\s:\\s)(\\w*)

Text : "type : text,"

It work exact as I want when I have just one whitespace before/after color...

"type_SPACE_:_SPACE_text

But if I have 2 spaces or none, it doesn't work.

I already try with this, but doesn't match. (?<=type\\s*:\\s*)(\\w*)

Also, I try with this, best approach. But with this, the matched text contain the colon.

(?<=type)(\\s*):(\\s*)(.*)(?=,)

To do the test I use gskinner's tester... http://gskinner.com/RegExr/

If you're doing this in C# and using the included Regex engine, your original regex should work, with a slight modification:

        string myString = "type :  something";

        var match = Regex.Match(myString, @"(?<=type\s*:\s*)\w+");

        Console.Write(match);

Edit: The reason why the ?<=type\\s*:\\s*)\\w* version wasn't working for you with multiple spaces, is because the regex match was happily returning various combinations of strings with 0 characters after the variable number of spaces following the colon.

You can view the various matched strings by using Regex.Matches , you'll see that your matched word is in there, but it's not the first result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM