-1

I have the following string:

root.single_product.baby->teenager_furn.others.

I want to extract each string in front of the .-sign. I have came up with this:

Regex.Matches("root.single_product.baby->teenager_furn.others.", @"([\w]+)");

This gives me the following list:

root
single_product
baby
teenager_furn
others

But I want the following instead:

root
single_product
baby->teenager_furn
others

How can I accomplish this?

4
  • 1
    I'm pretty sure you're looking for Regex.Split or the normal string.Split function. Commented Dec 20, 2019 at 12:38
  • @mypronounismonicareinstate and how should the pattern be? Commented Dec 20, 2019 at 12:40
  • @Bryan Use the pattern \. for Regex.Split or just the character . for string.Split Commented Dec 20, 2019 at 12:40
  • 1
    @Bryan 'string [] split = myString.Split('.');' Commented Dec 20, 2019 at 12:42

2 Answers 2

2

Your current regex is matching "everything that is a word character". -> are not word characters, so they are not matched. Your regex doesn't seem to say much about matching things before the dot. If you want it to stay very permissive like this, you can do one of the following:

  • word characters, as well as - and >:

    [\w->]+
    
  • everything that is not a dot:

    [^.]+
    

If you want a regex that strictly matches things before dots, you can do something like:

[^.]+(?=\.)

or

([^.]+)\.

and extract group 1.

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

Comments

1

Is it important for you to use Regex? Why you not use string.Split method? Here is example: https://dotnetfiddle.net/HQgFwm

Or you can use Regex.Split with pattern "(.)" as here https://dotnetfiddle.net/y6AGVY

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.