search

Home  >  Q&A  >  body text

php - How to use regular matching to find words that do not contain a certain word (multiple characters, not the beginning or the end)

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.

習慣沉默習慣沉默2744 days ago825

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-24 11:34:15

    js
    /^(?!.*wtmp)/

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

    reply
    0
  • 为情所困

    为情所困2017-05-24 11:34:15

    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 ​​

    reply
    0
  • Cancelreply