c# - Extract string from a pattern preceded by any length -


i'm looking regular expression extract string file name

eg if filename format "anythingatallanylength_123_testname.docx", i'm interested in extracting "testname" ... fixed length of 8. (btw, 123 can 3 digit number)

i think can use regex match ... ".*_[0-9][0-9][0-9]_[a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z].docx$"

however matches whole thing. how can "testname"?

thanks

use parenthesis match specific piece of whole regex. can use curly braces specify counts of matching characters, , \d [0-9].

in c#:

var myregex = new regex(@"*._\d{3}_([a-za-z]{8})\.docx$"); 

now "testname" or whatever 8 letter piece found in captures collection of regex after using it.

also note, there performance overhead look-ahead , look-behind, presented in other solutions.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -