Home > Article > Backend Development > Filter words in English articles
This is an English string
<code>When I was young,my uncle king of the jungle,one day he call me,and say we walk to the river,i asked why? my uncle say, you must learn how to cherish the water.</code>
I want to write a regular expression that will work as long as that word
This is what I wrote.
<code>[^a-zA-Z]young[^a-zA-Z]</code>
But I can’t find the first word of the article
Online Regular Expressions
This is an English string
<code>When I was young,my uncle king of the jungle,one day he call me,and say we walk to the river,i asked why? my uncle say, you must learn how to cherish the water.</code>
I want to write a regular expression that will work as long as that word
This is what I wrote.
<code>[^a-zA-Z]young[^a-zA-Z]</code>
But I can’t find the first word of the article
Online Regular Expressions
[^a-zA-Z]
Change it to [^a-zA-Z]?
That’s it
<code>(^|[^a-zA-Z])you([^a-zA-Z]|$) </code>
(^|[^a-zA-Z])When([^a-zA-Z]|$)
I think this is ok, considering the first word and the last word, actually I I think the easiest way is to add a space or other special character to the head and tail of the string, so that your original regular expression can be successfully matched.
[^a-zA-Z]
This pattern matches a character that is not "a to z or A to Z". There is no character in front of When, so naturally it cannot be matched. Other matched words also match a space.
If you don’t want spaces, you can actually use the escape character b to match word boundaries. This escape character can match the beginning, end, and word boundaries formed by spaces and punctuation. It will not match any extra characters.
bWhenb
That’s it.