正则表达式获取字符串 [英] Regular Expression to get strings

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

问题描述

string str ="my first statement xxx my second statement xxx my third statement";




我应该如何基于搜索"xxx"之间的字符串编写正则表达式以获取上面列出的三个语句




how shall i write a regular expression to get three statements listed above based on searching strings between "xxx"

推荐答案

任务,只需使用 String.Split [
You don''t even need regular expressions for such a task, just using the String.Split[^] method is enough.


如果您的情况建议您使用 CPallini 的解决方案此处 [
If your case isn''t more complex than stated in your question I''d advise you to go with CPallini''s solution here[^].

Just in case you''d like to know anyhow how a regular expression would look like to achieve your goal:

(.*?)(xxx|


)



该表达式已通过免费工具
Regular Expression Designer [



This expression was verified to work with the free tool Regular Expression Designer[^].

It will give you four matches instead of three where the last match is the empty statement followed by the end of line. This is about the simplest regular expression I could come up with to achieve your goal.

Regards,

Manfred

P.S.: If anybody has a shorter variant please let me know. :)


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

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