Does anyone know how I can base my re condition based on it not finding a match. I have the following at the moment .....

if re.search(r'123', os.uname()[1]):

Dani AI

Generated

originally checked a regex against the system name; pointed out a simpler substring test. Both approaches are valid — choosing between them depends on whether the thing you are looking for is a literal substring or a pattern, and whether you need portability and performance.

Regex behavior in short: a search call returns a match object on success and None on failure, so you can test for absence by checking for None (or using not on the call). For repeated checks, compile the pattern once and reuse it to avoid the per-call compile cost.

import re

pattern = re.compile(r'your_pattern')
m = pattern.search(hostname)
if m is None:
    # no match — handle it here

If the item you're testing is a fixed literal (not a pattern), prefer a plain substring test: it is simpler and faster than invoking the regex engine. When you must treat user input as a literal inside a regex, escape it first to avoid accidental metacharacter behavior.

import re

pattern = re.compile(re.escape(literal_string), re.I)  # literal match, case-insensitive

Note: os.uname() is Unix-specific; for cross-platform code consider platform.node() or socket.gethostname() instead. See the Python docs for details on regex behavior and platform hostname helpers: re module docs and platform.node().

Recommended Answers

All 2 Replies

if '123' not in os.uname()[1]:

Many Thanks,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.