Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

regular expression match empty string

Hello -

I need a regular expression that will tell me if the string I am looking at is empty.

Thanks,
DT
0
DTwined
Asked:
DTwined
  • 2
  • 2
1 Solution
 
brutaldevCommented:
The following expression expects nothing to be matched (an empty string).
 
^$

Open in new window

0
 
käµfm³d 👽Commented:
__NO POINTS__

... provided you don't turn on the multi-line modifier.
0
 
DTwinedAuthor Commented:
The string is one line.
0
 
brutaldevCommented:
@DTwined: Because you are using a single line string the above code will work. Depending on your programming language though, there are probably better ways to do this with trimming and checking for zero length. Some languages provide helper methods like string.IsNullOrEmpty and string.IsNullOrWhiteSpace in .NET.

@kaufmed: If the string was multi-line then it actually contains characters (carriage returns and/or line feeds). The above regular expression means it genuinely empty.
0
 
käµfm³d 👽Commented:
If the string was multi-line then it actually contains characters
I didn't say the string would be multi-line; I said the multi-line modifier is turned on, which changes the meaning of ^ and $. However, when I tested it didn't seem to make a difference, so my comment above may be moot anyway  = )
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now