[英]Regex first character before word
我有以下文字:
Aida [09/01/2019 11:24:17]:這只是我可以幫助你的一些事情。 Aida [09/01/2019 11:24:18]:您可以在右側閱讀有關流程的更詳細說明。 員工[09/01/2019 11:24:23]:無法登錄bolanAida [09/01/2019 11:24:25]:用戶ID是什么?員工[09/01/2019 11:24] :28]: x0000yAida [09/01/2019 11:25:21]: Bolån 生產帳戶 x0000y 現已啟用。 Aida [09/01/2019 11:25:23]:您可以在右側閱讀有關流程的更詳細說明。 Aida [09/01/2019 11:44:43]:此對話已關閉。
在像 Aida 或 Employee 這樣的詞之前有一個字符的情況很少出現(詞之間沒有空格)
bolanAida,x0000yAida,ID?員工
我想在全文中的這些單詞之間添加空格。
bolan Aida, x0000y Aida, ID? 員工
也許您對如何實現這一點有任何正則表達式的想法?
提前致謝
這種事情可以通過反向引用和捕獲組來完成。 根據您使用的語言,您必須調整這個 ruby 示例:
> string = 'bolanAida, x0000yAida, ID?Employee Aida'
=> "bolanAida, x0000yAida, ID?Employee Aida"
> string.gsub( /(\S)Aida/, '\1 Aida')
=> "bolan Aida, x0000y Aida, ID?Employee Aida"
捕獲組是 (\\S),選擇任何非空格字符。 這在 ruby 中用 \\1 反向引用,但在其他語言中,這可能是 $1 或 regex-group(1)
import re
str = 'Aida Employee bolanAida, x0000yAida, ID?Employee Aida'
print re.sub(r'(?<=\S)(?=Aida|Employee)', ' ', str)
輸出:
Aida Employee bolan Aida, x0000y Aida, ID? Employee Aida
解釋:
(?<=\S) # positive lookbehind, make sure we have a non space before
(?=Aida|Employee) # positive look ahead, make sure we have Aida or Employee after
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.