Find a word not surrounded by alpha char

After some searching, this seems harder than I thought: I'm trying to write a regular expression in Python to find a word that is not surrounded by other letters or dashes.

In the following examples, I am trying to match ios:

  • It seems carpedios
  • I like "ios" because they have blue products.
  • I like carpedios and ios
  • I like carpedios and ios.
  • I like carped-ios

Matches should be as follows:

  • 1: do not match because iosafter d.
  • 2: match because it is iosnot surrounded by letters.
  • 3: match, because one of is iosnot surrounded by letters.
  • 4: match because one of is iosnot surrounded by letters.
  • 5: , ios -.

?

+4
3

:

(?<!-)\bios\b(?!-)

Regular expression visualization

Debuggex

+5

\b . , -, , , . :

[^\b-]

. [] - . ^ , , . , - (, , ) , . ( [0-9a-fA-F] .)

! :

$ cat t.txt
It seems carpedios
I like "ios" because they have blue products
I like carpedios and ios
I like carpedios and ios.
i like carped-ios

, :

$ grep '[^\b-]ios[^\b-]' t.txt
I like "ios" because they have blue products
I like carpedios and ios
I like carpedios and ios.

, !

: , , , .

+1

Sort of: [^a-zA-Z\-](ios)[^a-zA-Z\-]

Perhaps this will be problematic at the beginning or end of the line

-1
source

Source: https://habr.com/ru/post/1629581/


All Articles