2

I need help to built regular expression for

string which does not start with pcm_ or PCM_

any guess!!!

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0cool
  • 683
  • 2
  • 10
  • 27

6 Answers6

5

No need to use regular expression. Use String.startsWith() method.

if (!str.StartsWith("pcm_",StringComparison.InvariantCultureIgnoreCase)) {}
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
2
if (String.startsWith("pcm_") || String.startsWith("PCM_"))
{
    //...
}
gabsferreira
  • 3,089
  • 7
  • 37
  • 61
1

see similar link

Regex pattern for checking if a string starts with a certain substring?

Community
  • 1
  • 1
Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101
  • This would allow "!pcm_" and "!PCM_" at the start of the string. – stema Jan 19 '12 at 12:38
  • Your modification hasn't improved the expression. "!" is in a regex just the character "!" and not a NOT operater, there is also no AND operator in regex. – stema Jan 19 '12 at 12:45
1

The regex solution would be

^(?i)(?!pcm_)

(?i) is the inline version of RegexOptions.IgnoreCase

^ matches the start of the string

(?!pcm_) is a negative lookahead assertion, that is true if the string does not start with "pcm_" or "PCM_" (but also "PcM_, ...)

stema
  • 90,351
  • 20
  • 107
  • 135
1

As already pointed out, you don't need to use regular expressions for this, but if you wanted to you could use one with negative lookahead like so: ^(?!pcm_|PCM_).*$

jfiskvik
  • 635
  • 9
  • 14
0

No need for a Regex here, simply use String.StartsWith http://msdn.microsoft.com/en-us/library/system.string.startswith.aspx

Guillaume Slashy
  • 3,554
  • 8
  • 43
  • 68