asp.net 如何使用正则表达式拆分,用单词而不是.?!标记拆分文本?

ubof19bj  于 2022-11-26  发布在  .NET
关注(0)|答案(2)|浏览(162)

我想用不同的单词而不是句点来拆分文本。下面是我尝试的代码:

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

我想使用这些词或短语作为分隔符在文本中,而不是句号标记,这是一个例子什么输出我需要:

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

 another example-

文字:bla now blahhh
输出:现在blahhh

cnjp1d6j

cnjp1d6j1#

  • 您可以使用String.Contains MethodString.Substring Method来执行此操作。
  • 密码:*
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)));
  }
}
  • 输出:*
because bblahhh balh and blahhh
and blahhh
kq0g1dla

kq0g1dla2#

你可以试着找到\because\and\now\this is的位置,然后使用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
            }

输出量:

because bblahhh balh and blahhh

相关问题