0

I have a string like this:

++ ST/-- OP/22 SomeString/21 AnotherString/-- CL/++ ED

"++ ST" and "++ ED" are open-close document marks,
"-- OP" and "-- CL" are open-close for piece of info,
"/" - is kind of separator,
"22" and "21" are identification of variable strings.

So I need to somehow extract variables identified as 22 and 21 ("SomeString" and "AnotherString" in that example) with using regular expressions. Is it even possible to do so with string like this?

1
  • Do you need the identifications too or just a list of the strings? Can we assume that anything starting with ++ or -- can be ignored? Are the identifications always going to be numbers? Commented Nov 22, 2011 at 14:20

2 Answers 2

1

You don't need regular expressions for this, at least with that example.

If you know the possible delimiters and that the variables in question will always have delimiters between them and be separated with a space (as in your example), string.Split will do.

string.Split has an overload that takes a list of strings to split on which you can use.

var separators = new string[] {"++ ST", "++ ED", "-- OP", "-- CL", "/", " "};
var res = "++ ST/-- OP/22 SomeString/21 AnotherString/-- CL/++ ED"
          .Split(separators, StringSplitOptions.RemoveEmptyEntries);

res[0] == "22"; // true
res[1] == "SomeString"; // true
res[2] == "21"; // true
res[3] == "AnotherString"; // true
Sign up to request clarification or add additional context in comments.

Comments

0

I think you should use String.Split() with the character '/' (if I understood you have all your line in a single variable)

Comments

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.