I've to face the following challenge which I would like to automatize using Regex:

I have to match this two mappings:

  1. GetSQLParameter("@switch", SqlDbType.TinyInt, 1), _ GetSQLParameter("@IDUtente", SqlDbType.Int), _

  2. sqlClient.AddParameterWithValue("switch", SqlDbType.TinyInt, user.IDUser); sqlClient.AddParameterWithValue("IDUtente", SqlDbType.Int, user.IDUser);

I would like to use 2 capturing group on the first code to get the name of the parameter and it's DB type.

Then I would like to analyze the second piece of code matching the name of the parameter and it's DB Type just to replace it.

Which kind of solution would you see as optimal to reach this objective?

I also asked for ready-made tools on our companion site Software Recommendations.

有帮助吗?

解决方案

Pseudo code

 # ==================================
 # GetSQLParameter\s*\(\s*"@\s*([^\s"]+)\s*"\s*,\s*([^,\s]+)\s*,

 GetSQLParameter \s* \( \s* 
 "@ \s* 
 ( [^\s"]+ )                 # (1), Param Name
 \s*
 "
 \s* , \s* 
 ( [^,\s]+ )                 # (2), DB Type
 \s* ,

 # Store:  
 # AryStruct['\1'].newName    = 'newname';
 # AryStruct['\1'].oldDBtype  = '\2';
 # AryStruct['\1'].newDBtype  = 'newtype';


 # ==================================
 # (sqlClient\.AddParameterWithValue\s*\(\s*"\s*)([^"\s]+)(\s*"\s*,\s*)([^,\s]+)(\s*,)

 (                           # (1 start)
      sqlClient \. AddParameterWithValue \s* \( \s* 
      " \s* 
 )                           # (1 end)
 ( [^"\s]+ )                 # (2), Param Name
 (                           # (3 start)
      \s* 
      "
      \s* , \s* 
 )                           # (3 end)
 ( [^,\s]+ )                 # (4), DB Type
 ( \s* , )                   # (5)

 # Replacement:   '\1' + AryStruct['\2'].newName + '\3' +  AryStruct['\2'].newDBtype + '\5' 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top