Writing a regular expression with a sequence of words containing "or"
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+)+)
精彩评论