我需要有关正则表达式的帮助 [英] i need help on regex

查看:90
本文介绍了我需要有关正则表达式的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好所有



i我有字符串



hello all

i am have string

string content = "Shoes|plant|tree|usek|setan|babi|anjing";





我想说两个字,即鞋子|植物,在我的搜索中只写鞋子





and I want to take two words, namely "Shoes | plant", in my search to write "shoes" only

string content = "Shoes|plant|tree|usek|setan|babi|anjing"
            string keyword = shoes;
            if (Regex.Match(content, @"\b" + keyword + @"\b", RegexOptions.Singleline | RegexOptions.IgnoreCase).Success)
            {
                string result = Regex.Match(content, keyword + @"$",RegexOptions.None).ToString(); ;
               
            } 





字符串结果应该是两个字,即鞋子|植物



他的代码如何?



the string result should be two words , namely "Shoes|plant"

how his code?

推荐答案

,RegexOptions.None).ToString();;

}
",RegexOptions.None).ToString(); ; }





字符串结果应该是两个字,即鞋子|植物



他的代码如何?



the string result should be two words , namely "Shoes|plant"

how his code?


试试这段代码。它会有所帮助。

try this code.hope it will help.
string content = "Shoes|plant|tree|usek|setan|babi|anjing";
            string keyword = @"^shoes\|plant";
            string result="";

            if (Regex.Match(content, keyword, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success)
            {
                result = Regex.Match(content, keyword, RegexOptions.Singleline | RegexOptions.IgnoreCase).ToString();

            }


假设您需要关键字及其后面的单词,请使用以下表达式: string pattern = String.Format(({0})\ | \w +,关键字)。并将此模式用作正则表达式中的搜索模式。其余的似乎很好。

通过这种方式你会找到鞋子<植物 usek | setan usek 无论那意味着什么。
Supposing you want the keyword and the word after it, use following expression: string pattern = String.Format("({0})\|\w+", keyword). And use this pattern as search pattern in the regular expression. The rest seems good.
This way you will find Shoes|plant for shoes, and usek|setan for usek whatever that means.


这篇关于我需要有关正则表达式的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆