regex - BBEdit Grep/Regex 查找并替换破折号后的部分

标签 regex grep bbedit

当我想在文章编号列中查找并删除以破折号 (-) 开头的部分以及后面的所有内容时,正则表达式应该是什么样子?

我正在使用 BBEdit 搜索和替换制表符分隔的 CSV 文件中的字符串(示例如下)。

"Article number"    "Name"  "Third Column"
"Shorts Artic"  "Swa..."    "2018-07-28"
"Shorts Artic-1"    "Swa..."    ""
"Shorts Artic-2-1"  "Swa..."    "https://test-domain.com/..."
"Shorts Artic-2-2-1"    "Sw..." ""
"Shorts Artic-2-2-2-2-1"    "Ba..." "-asd"
"Shorts Artic-2-2-2-2-2-1"  "Nus..."
"Shorts Artic-2-2-2-2-2-1-1"    "Lek.."
"0858-1"    "Jacket Blue.."
"0858-2-1"  "Jacket Re.."
"0858-2-2-1"    "Int..."
"0858-2-2-2-1"  "In..."
"0858-2-2-2-2-1"    "Int..."
"0858-2-2-2-2-2-1"  "Int..."
"0858-2-2-2-2-2-2-1"    "Int..."
"0858-2-2-2-2-2-2-2-1"  "In..."
"0858-2-2-2-2-2-2-2-2"  "In..."
"0858-2-2-2-2-2-2-2-1"  "In..." "6 107-124 cm"
"stl 31-35-1-1-1-1-2-2-2-1-1"   "In..."

"Shorts Artic-1" 会变成 "Shorts Artic"

"Shorts Artic-2-2-1" 会变成 "Shorts Artic"

"0858-2-2-2-2-2-2-2-2" 会变成 "0858"

最佳答案

你可以使用这个模式:

("[a-zA-Z 0-9]+)(?:-\d)+(?=")
  • ("[a-zA-Z 0-9]+) 匹配并捕获"、字母字符、空格和数字。
  • (?:-\d)+ 非捕获组。重复匹配并捕获-和数字。
  • (?=") " 的正面前瞻。

替换为:

 \1

你可以试试here这个模式.


对于更新后的文本文件,您可以使用:

^"((?:[a-zA-z]+ ?)+|[0-9]+)(?:-?\d)+(?=")

替换为:

"\1

你可以试试here这个模式.

关于regex - BBEdit Grep/Regex 查找并替换破折号后的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51884884/

相关文章:

bbedit - 使用 BBedit 将 CR 替换为 LF

Python - 如何使用正则表达式正确解析它?它解析除本地格式之外的所有 E.164

c# - 正则表达式匹配一段文本中的一个部分

linux - grep/根据第一个文件中的列表从第二个文件中获取字符串

eclipse - 在 Eclipse 中显示一些不可见/空白字符

css - 如何使用 AppleScript 获取类的计数?

javascript - 将字符串转换为表情符号

Java 和正则表达式 : Matching a substring that is not preceded by specific characters

linux - 仅使用正则表达式查找一行中的第一个匹配项

bash - 按行比较两个文件并从第一个文件中删除重复项