0

I have a string which is in the following format:

[0:2]={1.1,1,5.1.2}

My requirement here is to split the values inside curly braces after = operator, and store them in to a string array. I had tried to split part by using Substring() and IndexOf() methods, and it worked. But I needed a cleaner and elegant way to achieve this via regular expressions.

Does anybody having clues to achieve my requirement?

5
  • 1
    why split is not an elegant approach? Commented Mar 26, 2013 at 7:46
  • Can't you use Split(',')? Commented Mar 26, 2013 at 7:47
  • No he can't, because of the leading [0:2]={ and the trailing }. Commented Mar 26, 2013 at 8:00
  • 1
    @Stephan, are those set in String.Concrete() such that they can't be stripped, eg using String.JackHammer()? ;-) Commented Mar 26, 2013 at 8:19
  • -1 OP didn't advise he was also after validation of the input string (in fact, clearly stated the input string was of a given format). Nor did he respond to questions about why split couldn't be used, which would've explained the validation requirement. Commented Mar 26, 2013 at 9:27

4 Answers 4

2

Here is your fully RegEx solution:

Dim input As String = "[0:2]={1.1,1,5.1.2}"

Dim match = Regex.Match(input, "\[\d:\d\]={(?:([^,]+),)*([^,]+)}")

Dim results = match.Groups(1).Captures.Cast(Of Capture).Select(Function(c) c.Value).Concat(match.Groups(2).Captures.Cast(Of Capture).Select(Function(c) c.Value)).ToArray()

Don't think it is more readable then standard split:

Dim startIndex = input.IndexOf("{"c) + 1
Dim length = input.Length - startIndex - 1
Dim results = input.Substring(startIndex, length).Split(",")
Sign up to request clarification or add additional context in comments.

Comments

1

You could use a regular expression to extract the values inside the curly braces, and then use an ordinary Split:

Regex.Match("[0:2]={1.1,1,5.1.2}", "{(.*)}").Groups(1).Value.Split(","c)

Comments

0
Dim s As String = "[0:2]={1.1,1,5.1.2}";

Dim separatorChar as char = "="c;
Dim commaChar as char = ","c;
Dim openBraceChar as char = "{"c;
Dim closeBraceChar as char = "}"c;

Dim result() as String = 
  s.Split(separatorChar)(1)
   .trim(openBraceChar)
   .trim(closeBraceChar)
   .split(commaChar);

(assuming it works! Typed on an iPad so can't verify syntax easily, but principal should be sound).

EDIT: updated to VB as downvoted for showing working .net methods in c# syntax.

5 Comments

@SysDragon yes, as per my comment in my answer, the method calls/usage that answer the question are all available in .net. The specific language syntax is irrelevant would't you agree, if the answer helps the OP?
Downvoted? This answer is not useful, even though no one else recommended the trim methods?
@SysDragon it is now, although it's moot as OP wanted input validation too, which was not explained in the question (in fact it was implicitly stated that validation was not required, IMHO).
+1 For being adamant to remove the downvote by supplying the relevant answer. :)
@GLOIERTECH. Thanks mate I appreciate it :-) Still reckon a .net based answer should've been ok, the questions are often more about "how can I do this", rather than "type out the exact code". But yeah, you tagged as vb so fair enough!
-1

if you want it using Regex

Dim s() As String=Regex.match(str,"(={)(.*)(})").Groups(1).Tostring.split(',');

3 Comments

My downvote was because of your first version which did not even work (the one with the sentence "please don't vote down, i'm editing"). What's wrong with clicking on the post button when the answer is complete?
actually it my keyboard problem the enter key is locked i clicked tab button its posted it. so i written that im in the middle of editing.
Mine was coz the solution was wrong. Removed. Your outer groups capturing the braces, would be better as non capturing groups: ..."(?:={)(.*)(?:})").Groups(0)... Actually, they need not be grouped at all: ..."={(.*)}").Groups(0)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.