简体   繁体   English

正则表达式-将单词与句子匹配

[英]Regex - Match word against sentence

I'd like to write a regex query to be run on a set of elements. 我想编写一个正则表达式查询,以在一组元素上运行。

The query should return an element, as long as the search term matches the start of the word, and that all the words in the search term are in the match string. 只要搜索词匹配单词的开头,并且搜索词中的所有单词都在匹配字符串中,则查询应返回一个元素。

For example: 例如:

Search Term: "hell"
matches: "hello", "oh hello", "oh-hello"
doesn't match: "shell" - doesn't start the word out

My first attempt was to use BEGINSWITH[c] , however that wouldn't match "oh hello" from above. 我的第一次尝试是使用BEGINSWITH[c] ,但是从上面不能匹配“ oh hello”。 So I think I probably need to use MATCHES[c] %@ and use some sort of regex. 因此,我想我可能需要使用MATCHES[c] %@并使用某种正则表达式。 Anybody good with regex to have some ideas? 使用正则表达式的人有什么想法吗?

正则表达式

^hell[^ ]+|[^ ]+[ -]hell[^ ]+

I have used the below find anywhere that matches a word; 我在任何与单词匹配的地方使用了下面的查找;

\bHell(o|)?\b

i don't believe that is the best way as the ...(o|)? 我不认为这是...(o |)的最佳方法? is telling Regex to find where it matches Hell and then anywhere that 'Hell' has an o after it. 告诉正则表达式查找与地狱匹配的位置,然后在“地狱”之后带有o的任何位置。

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式匹配字符串的句子和单词 - Regex to match a sentence and word of a string 正则表达式匹配句子中的第一个单词 - Regex to match first word in sentence Rego 正则表达式以匹配句子中的特定单词 - Rego regex to match a specific word from a sentence 使用正则表达式匹配MYSQL中句子中的单词 - match a word from a sentence in MYSQL using regex 如何匹配此 Python 正则表达式中的单词或句子? - how to match either word or sentence in this Python regex? 正则表达式匹配单词中的a字符串,并且该单词必须出现在句子的最后一个单词 - Regex to match the a string in a word, and that word must appear at the last word of the sentence 检查给定正则表达式的句子,并将“&lt;”替换为“小于”单词 - Check sentence against the given regex and replace “<” with “less than” word PHP Regex将句子中的单词与其他某些特定单词匹配(可选) - PHP Regex to match word in a sentence with optionally only other certain words Javascript Regex-在单词中间(在句子中)匹配大写/大写字母吗? - Javascript Regex - match capital/upperacase letter in a middle of a word (in a sentence)? 正则表达式匹配句子的第二个单词并修剪前导空格 - Regex to match the second word of a sentence and trim leading white space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM