How to validate identifiers like c & c++ variables using regex?

By James J

validation of given string like c & c++ identifiers, which allow first as caharacter & then allow characters or digits or underscore...

private void button1_Click(object sender, EventArgs e)
{
          
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z](([a-zA-Z0-9]*)|(_[a-zA-Z0-9]_*)|(_[a-zA-Z0-9]*)|([a-zA-Z0-9]_*))$");
            
if (regex.IsMatch(textBox1.Text))
            {
                 MessageBox.
Show("Value is correct.");
            }
            
else
            {
                 MessageBox.
Show("Value is not correct.");
            }
}

How to validate identifiers like c & c++ variables using regex?  (1853 Views)