简体   繁体   English

正则表达式匹配开始和结束

[英]Regex that matches beginning and end

I am trying to find a regex that matches words that start with ch and end in jane. 我试图找到一个正则表达式,以匹配以ch开头并以简结尾的单词。 The program I am searching in is java-based, so that may be why none of the things I've tried so far worked for me??? 我正在搜索的程序是基于Java的,所以这可能就是为什么到目前为止我没有尝试过的任何事情对我来说都有效的原因??? I know that there are already threads with the same topic, but I couldn't resolve my problem so far. 我知道已经有相同主题的主题,但到目前为止我仍无法解决问题。

I have tried so far: 到目前为止,我已经尝试过:

/^ch\..*jane$/

^ch\..*jane$

\bch\.\w+jane\b

and some related to that (trial and error...) that do not give me any results at all. 还有一些与之相关的(尝试和错误...)根本没有给我任何结果。

There are two that work: 有两个工作原理:

\bch.*jane\b 

^ch(.*)jane$

but they give me more results than I want, ie, but a string of words where the first one starts in ch and the last one ends in jane in addition to single words staring with ch and ending in jane. 但是它们给我的结果比我想要的要多,也就是说,除了一个单词以ch开头并以jane结尾之外,一串单词的第一个单词以ch开头,最后一个单词以jane结尾。 I am looking for single words only! 我只在寻找单个单词!

Can any of you help me? 你们有谁能帮我吗? Thanks! 谢谢!

Do you want beginning of the string or word boundary? 您要字符串或单词边界的开头吗?

Word boundary you use \\bmyregex-word\\b , there shall not be any spaces and ^my.*regex$ matches the entire string such that if the string dies not start with my and ends with regex , it will always fail to match 使用\\bmyregex-word\\b单词边界,不得有任何空格,并且^my.*regex$匹配整个字符串,因此,如果字符串以my开头而不以regex结尾, regex它将始终无法匹配

Now to find words use: \\bch\\w*jane\\b 现在要查找words使用: \\bch\\w*jane\\b

You could try the below regex to match the words which starts with ch and ends with jane , 您可以尝试使用以下正则表达式来匹配以ch开头和以jane结尾的单词,

(?=^ch.*)(?=.*jane$).*

DEMO 演示

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM