c# - Find string with RegEx by prefix and segments -


we trying write program stores variables, parameters, methods, classnames etc. of project. replace methods right names. using directory.getfiles() , streamreader read out .cs files. let's following result got reading out .cs files:

string code = @"public static void _met_classname_methodname() { var _var_methodname_foo = \"test\"; }"; 

in reality string code way longer , contains more method , variable definitions. in end going add parameters , class names.

i trying method , variable name regex although nothing worked , have no idea how achieve trying. symbol names consist of following: _abc_segment1_segment2. abc can 3-letter value, segment1 , segment2 contain alphanumeric characters (the length of segments variable) , underscores there.

i tried reading on regex , used regexr.com "try out" although never got far. appreciated.

the following tried:

/_met_([a-z]\w+)/g 

this isn't variable due having _met_ in there fixed , doesn't make difference in segment1 , segment2.

the following it:

_[a-za-z]{3}_[a-za-z0-9]*_[a-za-z0-9]* 

breakdown

_                 //match first underscore [a-za-z]{3}       //match 3 alpha characters _                 //match separator underscore [a-za-z0-9]*      //match alpha-numeric string (segment1) _                 //match underscore [a-za-z0-9]*      //match alphanumeric string (segment2) 

alternatively, since looks first part (the met or var) small subset of acceptable values, following work if want match few values in part of string:

_(met|var)_[a-za-z0-9]*_[a-za-z0-9]* 

the (met|var) part matches either met or var.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -