Home > Backend Development > PHP Tutorial > Filter words in English articles

Filter words in English articles

WBOY
Release: 2016-08-26 10:28:23
Original
1969 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

But I can’t find the first word of the article
Filter words in English articles

Online Regular Expressions

Reply content:

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

But I can’t find the first word of the article
Filter words in English articles

Online Regular Expressions

[^a-zA-Z] Change it to [^a-zA-Z]? That’s it

<code>(^|[^a-zA-Z])you([^a-zA-Z]|$)
</code>
Copy after login

Filter words in English articles

(^|[^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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template