PC Review


Reply
Thread Tools Rate Thread

converting RegularExpressions.GroupCollection to array of strings

 
 
Analysis&Solutions
Guest
Posts: n/a
 
      28th Oct 2010
I have a situation where I use regular expression subpattern matches to
construct new strings. In some cases, the values need to be tweaked
before constructing the new string. In Visual Basic, the match groups
are read only:

' Property 'Value' is 'ReadOnly'.
mtch.Groups.Item(1).Value = "foo"


So I want to assign the values to a temporary array of strings. But each
way I try to do so results in error messages or warnings saying it's not
doable:

' Option Strict On disallows implicit conversions from
' 'System.Collections.Generic.IEnumerable(Of String)'
' to '1-dimensional array of String'.
Dim baz() As String = mtch.Groups.Cast(Of String)()

' Value of type 'System.Text.RegularExpressions.GroupCollection'
' cannot be converted to 'String'.
Dim foo() As String = DirectCast(mtch.Groups, String)

' Value of type 'System.Text.RegularExpressions.GroupCollection'
' cannot be converted to 'System.Array'.
Dim bar() As String = DirectCast(mtch.Groups, Array)


Is there a way to do what I want, please?

Of course, I could loop over each group element and put the value in an
array manually, but this is less than elegant. I've searched all over the
net and have had no luck.

Thanks,

--Dan
--
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
data intensive web and database programming
http://www.AnalysisAndSolutions.com/
4015 7th Ave #4, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409
 
Reply With Quote
 
 
 
 
Andrew Morton
Guest
Posts: n/a
 
      28th Oct 2010
You can use LINQ and extension methods:

Dim xs As System.Text.RegularExpressions.GroupCollection
Dim ss() As String = (From x In xs Select (x.ToString)).ToArray

--
Andrew


 
Reply With Quote
 
Analysis&Solutions
Guest
Posts: n/a
 
      28th Oct 2010
In <(E-Mail Removed)> "Andrew Morton" <(E-Mail Removed)> writes:

>Dim xs As System.Text.RegularExpressions.GroupCollection
>Dim ss() As String = (From x In xs Select (x.ToString)).ToArray


Works like a charm. Many thanks,

--Dan
--
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
data intensive web and database programming
http://www.AnalysisAndSolutions.com/
4015 7th Ave #4, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting System.Text.RegularExpressions regularexprssion to RegularExpressionValidator.ValidationExpression Vikram Microsoft Dot NET 0 29th Nov 2005 06:06 AM
Easy way to cast an array of strings to an array of objects? ssg31415926 Microsoft C# .NET 3 30th Aug 2005 08:39 PM
Converting a double array to byte array =?Utf-8?B?UmFncw==?= Microsoft Dot NET Framework 5 10th Aug 2005 01:22 PM
is there way to convert array of objects to array of strings ? roni Microsoft Dot NET 6 29th Apr 2005 02:23 PM
PN: Converting C Strings to managed strings Peter Nolan Microsoft VC .NET 0 20th Nov 2003 11:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:39 AM.