RegEx question How to string match to New String?
-
Hi everyone, I have a string like: "[FormField width=30 type=upload]" Using RegEx, I want to take the value of "type", and of width, and output them to their own seperate strings. How do I do this? I thought I might be able to do this using Regex.Match, but that just modifies the original string, not write them out to new strings. Any help with this is greatly appreciated. Thank you!
-
Hi everyone, I have a string like: "[FormField width=30 type=upload]" Using RegEx, I want to take the value of "type", and of width, and output them to their own seperate strings. How do I do this? I thought I might be able to do this using Regex.Match, but that just modifies the original string, not write them out to new strings. Any help with this is greatly appreciated. Thank you!
Use a regex like: @"width=(\d+) type=(.+)]"; if this matches, look in the match.Groups[0] for the first value, and [1] for the second value. You can also use named captures: @"width=(?\d+) type=(?.+)]"; and then use match.Groups["width"] and ["type"]. That really helps the readability of code.