【问题标题】:extract bold and italic text from a text document从文本文档中提取粗体和斜体文本
【发布时间】:2021-11-23 09:51:26
【问题描述】:

我有文本文件,我正在以粗体和斜体突出显示某些文本。我想要一个读取 .txt 文件并将所有粗体或斜体文本导出到另一个文档(文本文件)的脚本。

有人知道方法吗?

最好是R方案,但可以尝试其他方案。

Mac 用户

【问题讨论】:

  • .txt 文件通常是没有任何格式信息的纯文本文件,例如斜体或粗体。您使用的是哪种文件格式?
  • 在文本编辑器中使用 markdown 语法。基本上我的想法是把pdf转成文本来阅读教科书,我将每章的关键点加粗斜体,当我完成后我想将关键点导出到一个关于该主题的wiki。

标签: r macos text


【解决方案1】:

假设我们有一个 markdown 格式的文本文件 ìn.md,我们想创建另一个 markdown 文件 out.md,其中只包含斜体和粗体部分。

.md 中的文件内容:

# Header

There is *italic* and **bold** text!
There is *another italic* and **another bold** text!
library(tidyverse)

text <- read_file("in.md")
bold_texts <- text %>%
  str_extract_all("\\*\\*[^\\*]+\\*\\*") %>%
  purrr::simplify() %>%
  map_chr(~ .x %>% str_remove_all("\\*"))
bold_texts
#> [1] "bold"         "another bold"
italic_texts <-
  text %>%
  str_remove_all(bold_texts %>% map_chr(~ paste0("\\*\\*", .x, "\\*\\*")) %>% paste0(collapse = "|")) %>%
  str_extract_all("\\*[^\\*]+\\*") %>%
  purrr::simplify() %>%
  map_chr(~ .x %>% str_remove_all("\\*"))
italic_texts
#> [1] "italic"         "another italic"

out_text <- c("#Bold texts:", bold_texts, "#Italic texts:", italic_texts) %>% paste0(collapse = "\n")
cat(out_text)
#> #Bold texts:
#> bold
#> another bold
#> #Italic texts:
#> italic
#> another italic
write_file(out_text, "out.md")

reprex package (v2.0.1) 于 2021 年 11 月 23 日创建

【讨论】:

  • 谢谢,完美运行!
猜你喜欢
  • 2013-02-24
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多