我有文本檔案,我以粗體和斜體突出顯示某些文本。我想要一個腳本,它讀取 .txt 檔案并將所有粗體或斜體文本匯出到另一個檔案(文本檔案)中。
有人知道方法嗎?
最好是 R 解決方案,但可以嘗試其他解決方案。
Mac用戶
uj5u.com熱心網友回復:
假設我們有一個 Markdown 格式的文本檔案,ìn.md并且我們想要創建另一個out.md僅包含斜體和粗體部分的Markdown 檔案。
.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 包(v2.0.1)于 2021 年 11 月 23 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/364207.html
