精确字符串 coldfusion 正则表达式
Exact string coldfusion regular expression
我正在使用正则表达式来替换所有不等于确切单词 "NULL" 的字符,并保留所有数字。我做了第一步,将我的字符串中的所有 "NULL" 个单词替换为:
<cfset data = ReReplaceNoCase("123NjyfjUghfLL|NULL|NULL|NULL","\bNULL\b","","ALL")>
它会删除所有 "NULL" 单词的实例,这意味着它不会从子字符串“123NjyfjUghfLL”中删除字母 "N"、"U" 和 "L"。这是正确的。但现在,我想扭转这一局面。我只想保留 "NULL" 个词,这意味着它会删除单个 "L"、"U" 和 "L"。所以我试过了:
<cfset data = ReReplaceNoCase("123NjyfjUghfLL|NULL|NULL|NULL","[^\bNULL\b]","","ALL")>
但现在这会保留所有 "N"、"U" 和 "L" 字母,因此它会输出 "NULLNULLNULLNULL"。应该只有3次"NULL".
有人可以帮我解决这个问题吗?在哪里添加额外的代码来保留数字?谢谢。
你可以做到这一点
<cfset data = ReReplaceNoCase("123NjyfjUghfLL|NULL|NULL|NULL","(^|\|)(?!NULL(?:$|\|))([^|]*)(?=$|\|)","","ALL")>
(^|\|)(?!NULL(?:$|\|))([^|]*)(?=$|\|)
解释:
( # Opens Capture Group 1
^ # Anchors to the beginning to the string.
| # Alternation (CG1)
\| # Literal |
) # Closes CG1
(?! # Opens Negative Lookahead
NULL # Literal NULL
(?: # Opens Non-Capturing group
$ # Anchors to the end to the string.
| # Alternation (NCG)
\| # Literal |
) # Closes NCG
) # Closes NLA
( # Opens Capture Group 2
[^|]* # Negated Character class (excludes the characters within)
# None of: |
# * repeats zero or more times
) # Closes CG2
(?= # Opens LA
$ # Anchors to the end to the string.
| # Alternation (LA)
\| # Literal |
) # Closes LA
最后,对性格的一些感悟类(方括号内的内容)
[^\bNULL\b]
的意思是
[^\bNULL\b] # Negated Character class (excludes the characters within)
# None of: \b,N,U,L
# When \b is inside a character class, it matches a backspace character.
# Outside of a character class, \b matches a word boundary as you use it in your first code.
字符 类 不是为匹配或忽略单词而设计的,它们是为允许或排除字符或字符范围而设计的。
编辑:
Ok so it works well. But what if I would like to keep also the digits? I am a kind of lost in this line of code and I cannot find where to put extra code... I think the extra code would be [^0-9] right?
这个正则表达式 (demo) 还允许任何长度的数字,其中数字是整个值
(^|\|)(?!(?:NULL|[0-9]+)(?:$|\|))([^|]*)(?=$|\|)
您也可以使用此正则表达式 (demo) 来允许带有十进制值的数字。
(^|\|)(?!(?:NULL|[0-9]+(?:\.[0-9]+)?)(?:$|\|))([^|]*)(?=$|\|)
我正在使用正则表达式来替换所有不等于确切单词 "NULL" 的字符,并保留所有数字。我做了第一步,将我的字符串中的所有 "NULL" 个单词替换为:
<cfset data = ReReplaceNoCase("123NjyfjUghfLL|NULL|NULL|NULL","\bNULL\b","","ALL")>
它会删除所有 "NULL" 单词的实例,这意味着它不会从子字符串“123NjyfjUghfLL”中删除字母 "N"、"U" 和 "L"。这是正确的。但现在,我想扭转这一局面。我只想保留 "NULL" 个词,这意味着它会删除单个 "L"、"U" 和 "L"。所以我试过了:
<cfset data = ReReplaceNoCase("123NjyfjUghfLL|NULL|NULL|NULL","[^\bNULL\b]","","ALL")>
但现在这会保留所有 "N"、"U" 和 "L" 字母,因此它会输出 "NULLNULLNULLNULL"。应该只有3次"NULL".
有人可以帮我解决这个问题吗?在哪里添加额外的代码来保留数字?谢谢。
你可以做到这一点
<cfset data = ReReplaceNoCase("123NjyfjUghfLL|NULL|NULL|NULL","(^|\|)(?!NULL(?:$|\|))([^|]*)(?=$|\|)","","ALL")>
(^|\|)(?!NULL(?:$|\|))([^|]*)(?=$|\|)
解释:
( # Opens Capture Group 1
^ # Anchors to the beginning to the string.
| # Alternation (CG1)
\| # Literal |
) # Closes CG1
(?! # Opens Negative Lookahead
NULL # Literal NULL
(?: # Opens Non-Capturing group
$ # Anchors to the end to the string.
| # Alternation (NCG)
\| # Literal |
) # Closes NCG
) # Closes NLA
( # Opens Capture Group 2
[^|]* # Negated Character class (excludes the characters within)
# None of: |
# * repeats zero or more times
) # Closes CG2
(?= # Opens LA
$ # Anchors to the end to the string.
| # Alternation (LA)
\| # Literal |
) # Closes LA
最后,对性格的一些感悟类(方括号内的内容)
[^\bNULL\b]
的意思是
[^\bNULL\b] # Negated Character class (excludes the characters within)
# None of: \b,N,U,L
# When \b is inside a character class, it matches a backspace character.
# Outside of a character class, \b matches a word boundary as you use it in your first code.
字符 类 不是为匹配或忽略单词而设计的,它们是为允许或排除字符或字符范围而设计的。
编辑:
Ok so it works well. But what if I would like to keep also the digits? I am a kind of lost in this line of code and I cannot find where to put extra code... I think the extra code would be [^0-9] right?
这个正则表达式 (demo) 还允许任何长度的数字,其中数字是整个值
(^|\|)(?!(?:NULL|[0-9]+)(?:$|\|))([^|]*)(?=$|\|)
您也可以使用此正则表达式 (demo) 来允许带有十进制值的数字。
(^|\|)(?!(?:NULL|[0-9]+(?:\.[0-9]+)?)(?:$|\|))([^|]*)(?=$|\|)