简体   繁体   中英

Regex that matches beginning and end

I am trying to find a regex that matches words that start with ch and end in jane. 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??? 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. 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

Now to find words use: \\bch\\w*jane\\b

You could try the below regex to match the words which starts with ch and ends with jane ,

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

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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