0

I need a regular expression to capture all text before a character, lower or upper case directly followed by a number from zero to nine. And if the code would be different the same but for more than one character in both lower or upper case. An example of the type of strings I am working with:

case 1: "This is what I want v1"

case 2: "This is what I want Vol.27"

desired result: This is what I want

I have it working except for being case insensitive:

result = Regex.Match(filename, @"(.*?)(?:\s+V[0-9]+|\s+Vol\.?\s*[0-9]+)").Groups[1].Value;

So close! Anyone know how to make it case insensitive?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Does case 2 also match? It has a `.` in addition to letters and numbers. If so what other characters are legal as well and under what constraints? – heijp06 Jul 05 '19 at 17:56

2 Answers2

0

One or more numbers can be represented by the set:

[0-9]+

So what you want is

[\s\w]+[^0-9]

This says [\s\w]+ - one or more whitespace character or word character not followed by any number 0 through 9.

check it out here

MichaelD
  • 1,274
  • 1
  • 10
  • 16
0

My guess is that maybe you want a bit more bounded expression, such as:

(.*?)(?:\s+v[0-9]+|\s+vol\.?\s*[0-9]+)

with an i flag.

Demo

.NET DEMO

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    This is perfect! However I can't seem to get it to work in .Net. I just need the syntax of this exact thing to work in .Net. – null_byte0x00 Jul 05 '19 at 18:29
  • I am still struggling, I'm not sure what an i flag is. So my code is now thus: Regex.Replace(filename, @"((.*?)(?:\s+v[0-9]+|\s+vol\.?\s*[0-9]+))", "\\1") This selects the entire string though. – null_byte0x00 Jul 05 '19 at 18:53