Runtime error with Regular Expression

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

Hi,

I have a table with the following column names:

BOOK, CCY, IR(1Y), IR(2Y), IR(1M), IR(2M), IR(3M), IR(6M)

I want to strip "IR" as well as "(" and ")" off those column names. I
want the column names to be

BOOK, CCY, 1Y, 2Y, 1M, 2M, 3M, 6M

I use regular expression as below:

column.ColumnName =
System.Text.RegularExpressions.Regex.Replace(column.ColumnName,
"^IR(*)", string.Empty);

However, this give me a runtime error: "parsing \"^IR(*)\" -
Quantifier {x,y} following nothing."

Could anyone advise how to get it fixed?
 
Den 01-04-2011 16:33, Curious skrev:
Hi,

I have a table with the following column names:

BOOK, CCY, IR(1Y), IR(2Y), IR(1M), IR(2M), IR(3M), IR(6M)

I want to strip "IR" as well as "(" and ")" off those column names. I
want the column names to be

BOOK, CCY, 1Y, 2Y, 1M, 2M, 3M, 6M

I use regular expression as below:

column.ColumnName =
System.Text.RegularExpressions.Regex.Replace(column.ColumnName,
"^IR(*)", string.Empty);

However, this give me a runtime error: "parsing \"^IR(*)\" -
Quantifier {x,y} following nothing."

Could anyone advise how to get it fixed?
Hi

You need to read up on "literal strings" and regular expression
captures. Your expression does not make sense since the "(*)" construct
specifies a capture consisting of the regular expression "*", which of
course is not a proper regexp in itself.

Instead, you could do something like the example provided below. The
regexp uses a literal string (to avoid typing excessive "\"'s) and a
single capture (to get the parenthesized items). The single capture
specifies something to the effect of "anything but a closing
parenthesis, any number of times". Have fun!

------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String[] table={"BOOK", "CCY", "IR(1Y)", "IR(2Y)",
"IR(1M)", "IR(2M)", "IR(3M)", "IR(6M)"};
System.Console.WriteLine("Hello, World!");

foreach (String s in table)
{
System.Console.Write("Entry: " + s + " => ");
// Bad:
//
System.Console.WriteLine(System.Text.RegularExpressions.Regex.Replace(s,"^IR(*)",
string.Empty));
// Better:

System.Console.WriteLine(System.Text.RegularExpressions.Regex.Replace(s,
@"^IR\(([^)]*)\)$", "$1"));
}
System.Console.WriteLine("Hit <CR>, please...");
System.Console.ReadLine();
}
}
}
 
Den 01-04-2011 16:33, Curious skrev:


I have a table with the following column names:
BOOK, CCY, IR(1Y), IR(2Y), IR(1M), IR(2M), IR(3M), IR(6M)
I want to strip "IR" as well as "(" and ")" off those column names. I
want the column names to be
BOOK, CCY, 1Y, 2Y, 1M, 2M, 3M, 6M
I use regular expression as below:
column.ColumnName =
System.Text.RegularExpressions.Regex.Replace(column.ColumnName,
"^IR(*)", string.Empty);
However, this give me a runtime error: "parsing \"^IR(*)\" -
Quantifier {x,y} following nothing."
Could anyone advise how to get it fixed?

Hi

You need to read up on "literal strings" and regular expression
captures. Your expression does not make sense since the "(*)" construct
specifies a capture consisting of the regular expression "*", which of
course is not a proper regexp in itself.

Instead, you could do something like the example provided below. The
regexp uses a literal string (to avoid typing excessive "\"'s) and a
single capture (to get the parenthesized items). The single capture
specifies something to the effect of "anything but a closing
parenthesis, any number of times". Have fun!

------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
     class Program
     {
         static void Main(string[] args)
         {
             String[] table={"BOOK", "CCY", "IR(1Y)", "IR(2Y)",
"IR(1M)", "IR(2M)", "IR(3M)", "IR(6M)"};
             System.Console.WriteLine("Hello, World!");

             foreach (String s in table)
             {
                 System.Console.Write("Entry: " + s + "=> ");
                 // Bad:
                 //
System.Console.WriteLine(System.Text.RegularExpressions.Regex.Replace(s,"^I­R(*)",
string.Empty));
                 // Better:

System.Console.WriteLine(System.Text.RegularExpressions.Regex.Replace(s,
@"^IR\(([^)]*)\)$", "$1"));
             }
             System.Console.WriteLine("Hit <CR>, please...");
             System.Console.ReadLine();
         }
     }}

-----------

--
Yours sincerely,

Jan H. Paulsen- Hide quoted text -

- Show quoted text -

In your example, did you replace a string that matches the pattern you
specified with a "$1" string?
 
Den 08-04-2011 22:33, Curious skrev:
Den 01-04-2011 16:33, Curious skrev:


I have a table with the following column names:
BOOK, CCY, IR(1Y), IR(2Y), IR(1M), IR(2M), IR(3M), IR(6M)
I want to strip "IR" as well as "(" and ")" off those column names. I
want the column names to be
BOOK, CCY, 1Y, 2Y, 1M, 2M, 3M, 6M
I use regular expression as below:
column.ColumnName =
System.Text.RegularExpressions.Regex.Replace(column.ColumnName,
"^IR(*)", string.Empty);
However, this give me a runtime error: "parsing \"^IR(*)\" -
Quantifier {x,y} following nothing."
Could anyone advise how to get it fixed?

Hi

You need to read up on "literal strings" and regular expression
captures. Your expression does not make sense since the "(*)" construct
specifies a capture consisting of the regular expression "*", which of
course is not a proper regexp in itself.

Instead, you could do something like the example provided below. The
regexp uses a literal string (to avoid typing excessive "\"'s) and a
single capture (to get the parenthesized items). The single capture
specifies something to the effect of "anything but a closing
parenthesis, any number of times". Have fun!

------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String[] table={"BOOK", "CCY", "IR(1Y)", "IR(2Y)",
"IR(1M)", "IR(2M)", "IR(3M)", "IR(6M)"};
System.Console.WriteLine("Hello, World!");

foreach (String s in table)
{
System.Console.Write("Entry: " + s + " => ");
// Bad:
//
System.Console.WriteLine(System.Text.RegularExpressions.Regex.Replace(s,"^I­R(*)",
string.Empty));
// Better:

System.Console.WriteLine(System.Text.RegularExpressions.Regex.Replace(s,
@"^IR\(([^)]*)\)$", "$1"));
}
System.Console.WriteLine("Hit<CR>, please...");
System.Console.ReadLine();
}
}}

-----------

--
Yours sincerely,

Jan H. Paulsen- Hide quoted text -

- Show quoted text -

In your example, did you replace a string that matches the pattern you
specified with a "$1" string?
Yup, that's the first "capture". You really need to read up on these
things - your questions are very basic...
 
Back
Top