Sumit Chourasia Sumit Chourasia - 1 month ago 9
C# Question

Regex Operation not returning specific values

I'm using following Regex Operation

(?:public\s|private\s|protected\s|internal\s|)?[\s\w]*\s+(?<methodName>\w+)\s*\(\s*(?:(ref\s|in\s|out\s)?\s*(?<parameterType>[\w\?\[\]]+)\s+(?<parameter>\w+)\s*,?\s*)+\)


it is working fine on method like

public ResponseViewModel ResponseDetails(
ResponseViewModelContext responseViewModelContext)


but if there is any List in either return type or parameter it is not matching as expected.

i.e following method is not working

public List<ResponesViewModel> ResponseDetails(
List<ResponesViewModelContext> responseViewModelContext)


Link for Online Regex Tried

Answer

You had to add the <> brackets characters to the regex.

(?:public\s|private\s|protected\s|internal\s|)?[\s\w<>]*\s+(?<methodName>\w+)\s*\(\s*(?:(ref\s|in\s|out\s)?\s*(?<parameterType>[\w<>\?\[\]]+)\s+(?<parameter>\w+)\s*,?\s*)+\)

Link for regex

Comments