Matchcollection

hello. is there any way for me to input match collection into array

@Kian

I assume you have a matchcollection from some RegEx work and you want to get it as an array.
Give A try on conversion with YourMatchColectionVar.toArray

1 Like

i have 2 assign with this formula
To: 1
Value: Regex.Matches(text,pattern, RegexOptions.Multiline)

To: 2
Value: Regex.Matches(text,pattern, RegexOptions.Multiline)

ill like to combine this 2 by putting them into an array isit possible

i tried assigning
To: match
Value: {1,2}
it didnt work as they say there is an error (For Each: Unable to cast object of type ‘System.Text.RegularExpressions.MatchCollection’ to type ‘System.Text.RegularExpressions.Match’.)

@Kian

with matches as your MatchCollection, to obtain an array of string:

Assign (Array of String)
arrMatches = matches.OfType(Of Match).Select(Function(m) m.Value.ToString).ToArray

1 Like


i have errors can u help me

Change the type of matches variable to Array<String>. I edited my post to reflect that more explicitly.

it does not work isit bcoz i have done it wrongly


this is the error

You have to change in your ForEach the ArgumentType to String (no longer a Match)


i try combining rate into the array but it has error

Please remove the curly braces


i remove the brackets but still it didnt work

@Kian

OK, I was still at your initial question.

I think you should rename this topic to MatchCollection to Array and validate my first answer and create another topic like “combining two arrays” because it might help others.

Anyway, for “combining” two array, use Zip method. In the “Function” part you can produce a lot a convenient combination. Be careful as result’s element count will be the same as the shortest array (see example at the end).

With arr1 and arr2 as two Array of String

Assign (IEnumerable Of Array Of String)
combined = arr1.Zip(arr2, Function(a, b) {a, b})

This is enough for iterating over the result with a ForEach. If you want an array:

Assign (Array Of Array Of String)
newArray = arr1.Zip(arr2, Function(a, b) {a, b}).ToArray

For this last one, if you have

letters = {"one", "two", "three", "four"}
digits = {"1", "2", "3"}
newArray = letters.Zip(digits, Function(letter, digit) {letter, digit}).ToArray

newArray will be {{"one", "1"}, {"two", "2"}, {"three", "3"}}

Can this work for combining more than 4 different matches together?