开发者

Writing a regular expression with a sequence of words containing "or"

开发者 https://www.devze.com 2023-02-28 17:42 出处:网络
I want to extract a sequence of words connected with with \"or\". For example, from \"there or is or a or p开发者_开发百科roblem with my computer\"

I want to extract a sequence of words connected with with "or". For example, from

 "there or is or a or p开发者_开发百科roblem with my computer"

I want to extract

"there or is or a or problem"

I have following regular expression

(("[^"]+"+|.[^\s*]+)\s+or\s+)+("[^"]+"+|.[^\s*]+)

but the expression is giving the following results:

"there or is", " a or problem or with"

breaking at single character. Anything wrong with the expression?


It what are connected are words spelled in alphabets, it can be this:

\w+(?:\s+or\s+\w+)*

this will return

"there or is or a or problem",  "with", "my", "computer"

If you really want only those that have at least one or in it, as in your example,

\w+(?:\s+or\s+\w+)+

will return

"there or is or a or problem"


Try the one below:

[\w\s]+or\s+\w+

Note, this will match the highlighted in the following:

there or is or a or problem with my computer or i am going crazy

But if you want there or is or a or problem , computer or i for the above, go with:

(\w+(?:\s+or\s+\w+)+)
0

精彩评论

暂无评论...
验证码 换一张
取 消