0

I want to split a text by different words and not by periods. Here is a code I tried:

string StringFromTheInput = TextBox1.Text;
string[] splichar1 = Regex.Split(StringFromTheInput, @"(?<=[\because\and\now\this is])");

I want to use these words or phrases as delimiter in text instead of period mark, this is an example what output I need:

 Text: Sentence blah blah blahh because bblahhh balh and blahhh
 Output: because bblahhh balh and blahhh

 another example-

Text: bla now blahhh

Output: now blahhh

5
  • I want to split a text into sentences but instead of period mark [.] I want text parts to be seperated by some words, a normal text split would be this: Regex.Split(StringFromTheInput, @"(?<=[\.!?])"); but I want this Regex.Split(StringFromTheInput, @"(?<=[\because\and\now\this is])"); Commented Nov 14, 2022 at 20:17
  • Now I got this error: parsing "(?<=(if|and|but|so|when)" - Not enough )'s. Commented Nov 14, 2022 at 23:19
  • I think the answer of @ZakariaNajim helps you. Commented Nov 15, 2022 at 12:24
  • Hi,is my answer helpful? Commented Nov 17, 2022 at 9:50
  • Yiyi You - partialy I applied only regular expresssions from your code cause I still need to use Regex.Split: heres is the code I used: string[] splichar1 = Regex.Split(StringFromTheInput, @"s(because|and|now|this is)\s"); this code whenever finds a word of these "because" "and" etc splits the part of a text but I want to split the text so that the splitted text involves the keywords "because" "and"' in the beginning of the splitted text -> for example because + splitted text; and + splitted text; I hope I could explain some... Commented Nov 18, 2022 at 6:39

2 Answers 2

2
  • You can do it with String.Contains Method and String.Substring Method.

Code:

string stringfromtheinput = "Sentence blah blah blahh because bblahhh balh and blahhh";
String[] spearator = {
  "because",
  "and",
  "now",
  "this is"
};

foreach(string x in spearator) {
  if (stringfromtheinput.Contains(x)) {
    Console.WriteLine(stringfromtheinput.Substring(stringfromtheinput.LastIndexOf(x)));
  }
}

Output:

because bblahhh balh and blahhh
and blahhh
Sign up to request clarification or add additional context in comments.

Comments

1

You can try to find the position of \because\and\now\this is,and then use substring.

string StringFromTheInput = TextBox1.Text;
 string str="";
            var match = Regex.Match(s, @"\b(because|and|now|this is)\b");
            if (match.Success)
            {
                var index=match.Index; // 25
                str = s.Substring(index);//str is what you want
            }

Output:

because bblahhh balh and blahhh

1 Comment

Yiyi You - partialy I applied only regular expresssions from your code cause I still need to use Regex.Split: heres is the code I used: string[] splichar1 = Regex.Split(StringFromTheInput, @"\s(because|and|now|this is)\s"); this code whenever finds a word of these "because" "and" etc splits the part of a text but I want to split the text so that the splitted text involves the keywords "because" "and"' in the beginning of the splitted text -> for example because + splitted text; and + splitted text; I hope I could explain some..

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.