0

I'm not to hot on regex, done a fair amount of googling but for some reason seem to be struggling on this. Basically I have a string as:

VALUE1,VALUE2,VALUE3,VALUE4

Now all want is a regex where I can say give me value 2 or give me value 3 etc.

I would have thought I could do something like this to get the 2nd value

,.*,,

This for last etc

,,,.*

There must be a simple solution to this...should also add this has to be done via regex!

9
  • 1
    What happens if one of your value contains a coma? Maybe you should look at using a proper CSV parser. Commented May 31, 2018 at 14:12
  • Ha just have to trust me they wont, need the regex :) Commented May 31, 2018 at 14:14
  • It depends on how the code using the regex uses the regex, e.g. "^.*?,(.*?),.*?,.*?$" would capture the second item. Commented May 31, 2018 at 14:58
  • and this would be fourth etc ^.*?,.*?,(.*?),.*?$ that is perfect ty sir :) Commented May 31, 2018 at 15:01
  • 1
    @MartinCooke Checkout this regex demo, and make sure your seat belt is fastened securely ^ ^. Commented May 31, 2018 at 15:25

1 Answer 1

3

I think all you need here is a regex split on the comma separator:

Dim input As String = "VALUE1,VALUE2,VALUE3,VALUE4"
Dim terms() As String = Regex.Split(input, ",\s*")
For Each term As String In terms
    Console.WriteLine(term)
Next

Demo

Note that I split on ,\s*, in case there might be some whitespace after each comma.

We could try to give you a raw regex which could extract a term in a given position, but even that would require knowing which tool you are using, to know how to access a capture group. Since you're using VB.net, splitting the string using a regex is probably the way to go.

Edit:

From the comments, perhaps the following regex pattern is what you are looking for:

^(?:.*?,){3}(.*?)(?=,|$).*$

Demo

Sign up to request clarification or add additional context in comments.

8 Comments

many thanks for your response, I know its a bit odd but I won't be able to use that example as the application takes xml parameters, these being the string and then the regex to pull from the string if that's makes sense. I can't add vb.net code to the application I can only give the string values and the regex to tell the third party application which values I want collected.
@MartinCooke Then you need to at least tell me which regex tool/language your third party would be using. Without telling us this, I'm afraid you are asking a question which needs to be closed as unclear.
the third party application is written in C#, we are aiming to inject this with string values and then the regex to tell it which values it should pick out, hope this helps :)
Doesn't really help. I need to know the exact API being used. Anyway, the answer I gave I believe is what most people with your problem in VB would actually want to use.
Sorry I didn't explain it better, do see Andrew Mortons response above and this appears to be working :)
|

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.