“$”在“apt-cache search Something$”中如何工作

5 apt regular-expression

我明白那个:

$ apt-cache search package_name
Run Code Online (Sandbox Code Playgroud)

将搜索名称和描述中的匹配项。

但这是如何运作的呢?

$ apt-cache search package_name$
Run Code Online (Sandbox Code Playgroud)

例如,

$ apt-cache search desktop
Run Code Online (Sandbox Code Playgroud)

将生成名称或描述中任意位置包含“desktop”的软件包的名称和简短描述列表。

那么,如果我理解正确的话,

$ apt-cache search desktop$
Run Code Online (Sandbox Code Playgroud)

应该生成一个子集,其中“desktop”是名称或描述中任何行中的最后一个单词。

但我发现了一个例子,xjig,它显示apt-cache search desktop,但不显示apt-cache search desktop$,即使根据描述apt-cache show xjig在描述的倒数第二行中有“桌面”:

Description-zh: X11 拼图游戏xjig 是一种尝试在屏幕上 尽可能精确地
复制拼图游戏的拼图游戏。
任何 GIF、JPEG 或 PPM 图像都可以加载并切成
碎片。目标(与任何拼图游戏一样)是重新组装
原始图片。

可以使用鼠标自由旋转图块、翻转(对于
双面拼图)甚至在桌面
上显示为形状窗口 (后者推荐使用快速机器/显卡!)。
/usr/share/games/xjig 中提供了 示例图像。

那么为什么xjig不包含在 的输出中apt-cache search desktop$呢?

slm*_*slm 4

apt-cache手册页:

search regex...
   search performs a full text search on all available package lists for the 
   POSIX regex pattern given, see regex(7). It searches the package names 
   and the descriptions for an occurrence of the regular expression and 
   prints out the package name and the short description, including virtual 
   package names. If --full is given then output identical to show is 
   produced for each matched package, and if --names-only is given then the 
   long description is not searched, only the package name is.

   Separate arguments can be used to specify multiple search patterns that 
   are and'ed together.
Run Code Online (Sandbox Code Playgroud)

请注意它指出regex(7)。因此,当您要求apt-cache搜索某些内容时,您需要为其提供正则表达式。正$则表达式中的 表示您想要匹配,锚定到字符串的末尾。

因此,如果我们给出了值,desktop$我们只会匹配位于desktop字符串末尾的字符串,例如:

  • 这是一个桌面
  • 这是一个很长的桌面字符串

但它不会匹配诸如以下的字符串:

  • 这是窗口中的桌面
  • 这是窗口中的另一个桌面

在您的情况下,xjig 描述中的字符串desktop虽然位于描述行的末尾,但并不位于描述字段值的末尾。

例子

这是 xjig 的描述。

Description-en: An X11 jigsaw puzzle
 xjig is a puzzle that tries to replicate a jigsaw puzzle on the screen
 as closely as possible. Any GIF, JPEG or PPM image may be loaded and cut
 into pieces. The goal (as with any jigsaw puzzle) is to reassemble the
 original picture.
 .
 Tiles may be freely rotated by use of the mouse, flipped (for
 double-sided puzzles) and even shown as shaped windows on the desktop
 (fast machine/video card recommended for the latter!). An example image
 is provided in /usr/share/games/xjig .
Run Code Online (Sandbox Code Playgroud)

如果我搜索所有以 . 结尾的字符串xjig .$

$ apt-cache search 'xjig \.$'
xjig - An X11 jigsaw puzzle
Run Code Online (Sandbox Code Playgroud)

请记住,描述的值只是一个在该点换行的长字符串,因此尽管“desktop”位于行的末尾,但它实际上并不位于描述值的末尾。

选择

如果您要查找单词,则可以查找两侧带有空格 ( ) 或单词边界 ( )desktop的字符串。\s\b

$ apt-cache search '\sdesktop\s' | grep xjig
xjig - An X11 jigsaw puzzle

$ apt-cache search '\bdesktop\b' | grep xjig
xjig - An X11 jigsaw puzzle
Run Code Online (Sandbox Code Playgroud)

了解正则表达式锚点

也许使用grep正则表达式查看锚点的作用会有所帮助。

桌面还没结束

$ echo "this is a desktop string" | grep "desktop$"
$
Run Code Online (Sandbox Code Playgroud)

桌面在最后

$ echo "this is a desktop" | grep "desktop$"
this is a desktop
Run Code Online (Sandbox Code Playgroud)

$对将匹配锚定到字符串末尾的补充就是^将匹配锚定到开头。

桌面不在开始处

$ echo "this is a desktop" | grep "^desktop"
$
Run Code Online (Sandbox Code Playgroud)

开始时的桌面

$ echo "desktop this is a desktop" | grep "^desktop"
desktop this is a desktop
Run Code Online (Sandbox Code Playgroud)