[英]RegEx for capturing and replacing all digits in a string except for special patterns
我有一個文本,其中數字以各種可能的方式出現。 例如,
text = "hello23 the2e are 13 5.12apples *specially_x00123 named 31st"
我想用'#'替換所有數字,但以*,單詞,下划線,任何字符和數字開頭的特殊模式中的數字除外,例如* \\ w + _ [az] \\ d +(即* specially_x00123) 。
我嘗試使用環顧四周語法和非捕獲組,但是找不到將其准確更改為以下內容的方法
text_cleaned = "hello## the#e are ## #.##apples *specially_x00123 named ##st"
我可以使用如下所示的模式:
p1 = r'\d(?<!\*\w+_\w+)'
然后,它抱怨像這樣; “向后看需要固定寬度的樣式”
我試圖使用非捕獲組:
p2 = r'(?:\*[a-z]+_\w+)\b|\d'
它取出特殊令牌(* specially_x000123)和所有數字。 我認為這是我可能會包含在解決方案中的內容,但是我找不到如何做。 有任何想法嗎?
您可能要做的是在捕獲組(\\d)
捕獲數字,並在替換檢查中使用第一個捕獲組的回調。
如果是組1,則用#
替換,否則返回匹配項。
由於\\w+
也匹配下划線,因此您可以使用反義字符類[^\\W_\\n]+
匹配單詞char,但下划線除外
\*[^\W_\n]+_[a-z]\d+\b|(\d)
import re
text = "hello23 the2e are 13 5.12apples *specially_x00123 named 31st"
pattern = r"\*[^\W_\n]+_[a-z]\d+\b|(\d)"
print (re.sub(pattern, lambda x: "#" if x.group(1) else x.group(), text))
結果
hello## the#e are ## #.##apples *specially_x00123 named ##st
一種選擇可能是將字符串拆分為star之前,然后為star之后。 表達式(\\d)
捕獲star之前的每個數字,我們可以簡單地使用#
替換它,然后將其與$2
:
(\d)|(\*.*)
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(\d)|(\*.*)"
test_str = ("hello23 the2e are 13 5.12apples *specially_x00123 named\n\n"
"hello## the#e are ## #.##apples *specially_x00123 named")
subst = "#\\2"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
const regex = /(\\d)|(\\*.*)/gm; const str = `hello23 the2e are 13 5.12apples *specially_x00123 named`; const subst = `#$2`; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log('Substitution result: ', result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.