He extraido de una pagina web una DT donde las columnas de los años que se extraen figuran como (ejemplos)Ejercicio 2020 Alt Text, Ejercicio 2021 Alt Text…y yo con lo que me quiero quedar es con los 4 digitos que hacen referencia al año.
No quiero poner cada año a mano, si no que quiero que este dato sea variable, ya que se solicitará cada vez un año diferente y asi no hay que hacer modificaciones a posteriori.
years As List(Of String) = (From column As DataColumn In YourDataTable.Columns.Cast(Of DataColumn)()
Let yearMatch As Match = System.Text.RegularExpressions.Regex.Match(column.ColumnName, "\d{4}")
Where yearMatch.Success
Select yearMatch.Value).ToList()
Save To: years (DataType: System.Collections.Generic.List(System.String))
Value to Save: (From column As DataColumn In YourDataTable.Columns.Cast(Of DataColumn)()
Let yearMatch As Match = System.Text.RegularExpressions.Regex.Match(column.ColumnName, "\d{4}")
Where yearMatch.Success
Select yearMatch.Value).ToList()
pass the datatable variable in the place of YourDataTable and the output will be of datatype List(String).
To print it in Message box or log message you can use below syntax: