php - How to use regular matching to find words that do not contain a certain word (multiple characters, not the beginning or the end)
習慣沉默
習慣沉默 2017-05-24 11:32:15
0
2
760

Off topic: When I was on Linux, I saw last | grep [a-zA-Z] | grep -v 'wtmp' | wc -l. I was wondering if I could do it with just one grep, so I thought about it. Use regular expression to match a certain line that does not contain a certain word (such as wtmp), but the result is not figured out.

習慣沉默
習慣沉默

reply all(2)
淡淡烟草味

js
/^(?!.*wtmp)/

> p1=/^(?!.*wtmp)/
/^(?!.*wtmp)/
> p1.test('abc123')
true
> p1.test('abc1wtmp23')
false
>
为情所困

Matching does not contain, this difficulty should be relatively large for regular expressions, because this condition may be true at this position, but it may not be true at the next position! However, regular expressions will return any string that satisfies pattern
For As an example of your question, I recommend using awk to implement:

last | awk '!=wtpm&&/[A-Za-z]/{num++}END{print num}'

awkIt can provide more flexible logical processing and is more readable. Compared with a single regular expression, I think awk is a better choice in the scenario in the question, but if it is implemented in other languages, you may have to refer to In other languages ​​

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!