Exact string expression for cold infusion

I am using a regular expression to replace all characters that are not equal to the exact word "NULL" and also keep all digits. I took the first step by replacing all the "NULL" words from my string with this:

<cfset data = ReReplaceNoCase("123NjyfjUghfLL|NULL|NULL|NULL","\bNULL\b","","ALL")>

      

It removes all instances of the exact word "NULL", which means it does not remove the letters "N", "U" and "L" from the substring "123NjyfjUghfLL". And it is right. But now I want to change that. I only want to keep the word "NULL" which means it removes single "L", "U" and "L". So I tried this:

<cfset data = ReReplaceNoCase("123NjyfjUghfLL|NULL|NULL|NULL","[^\bNULL\b]","","ALL")>

      

But now this keeps all the letters "N", "U" and "L", so it outputs "NULLNULLNULLNULL". There should be only 3 times "NULL".

Can someone help me with this please? And where to add an additional code to save numbers? Thank.

+3


source to share


1 answer


You can do it

<cfset data = ReReplaceNoCase("123NjyfjUghfLL|NULL|NULL|NULL","(^|\|)(?!NULL(?:$|\|))([^|]*)(?=$|\|)","\1","ALL")>

(^|\|)(?!NULL(?:$|\|))([^|]*)(?=$|\|)

      

Explanation:

 (                   # 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

      

demo Regex101.com

Finally, some idea about character classes (content between square brackets)

What does it [^\bNULL\b]

mean

 [^\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.

      



Character classes are not meant to match or ignore words, they are meant to allow or exclude characters or ranges of characters.

Edit:

Good, works well. But what if I wanted to keep the numbers? I lost this line of code a bit and I can't find where to add the extra code ... I think the extra code would be [^ 0-9] correct?

This regex ( demo ) also allows numbers of any length where number is the entire value

(^|\|)(?!(?:NULL|[0-9]+)(?:$|\|))([^|]*)(?=$|\|)

      

You can also use this regex ( demo ) to allow numbers with decimal values.

(^|\|)(?!(?:NULL|[0-9]+(?:\.[0-9]+)?)(?:$|\|))([^|]*)(?=$|\|)

      

+1


source







All Articles