0

I don't get how to create a regular expression that matches everything, except a fixed, single word. Can somebody help me?

Background: alias map for mailserver which redirects all mail to one user. /.*/ -> user1 produces a redirection loop, so i need to exclude user1 from the regexp.

Edit:

The regex should match everything, EXCEPT the single word user1. It should also match input that contains that word, i.e., user11.

i.e.: /.*/ without user1

Edit2:

POSIX Basic Regular Expressions

user236012
  • 265
  • 1
  • 2
  • 9
  • 2
    Can you give us some sample input text, and then describe what the expected output is? Also, what regex tool/language are you using? – Tim Biegeleisen Sep 01 '18 at 15:29
  • As Tim Biegeleisen said, what Tool/programming language are you using? There's a lot of different RegEx out there. – Poul Bak Sep 01 '18 at 16:46

3 Answers3

0

If you're okay with getting your value from the first group, you can use something like this:

(?:\buser1\b)?(.+?)(?:\buser1\b|$)

Demo.

This will match anything before or after the word user1 or everything if it doesn't exist.

0

With grep you can invert the match:

$ echo "user2
user3
user1" | grep -v '^user1$'
user2
user3

Or, with sed you can delete the match:

$ echo "user10
user2
user1" | sed -E '/^user1$/d'
user10
user2
dawg
  • 98,345
  • 23
  • 131
  • 206
0

You can use the ^(?!user1$). for checking purpose, you can use python as:

import re
if(re.match('^(?!user1$)','user11')):
  print('user11 matched')

if(re.match('^(?!user1$)','user1')):
  print('user1 matched')

if(re.match('^(?!user1$)','AnyThingElseBeforeuser1')):
  print('AnyThingElseBeforeuser1 matched')

the output is as you desire:

user11 matched

AnyThingElseBeforeuser1 matched