Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Representing identifiers using Regular Expression

Tags:

The regular definition for recognizing identifiers in C programming language is given by

letter -> a|b|...z|A|B|...|Z|_ digit -> 0|1|...|9 identifier -> letter(letter|digit)* 

This definition will generate identifiers of the form

identifier: [_a-zA-Z][_a-zA-Z0-9]*

My question now is how do you limit the length of the identifier that can be generated to not more than 31 characters. What changes need to be made in the regular definition or how to write a regular expression to limit it to not more than the specified length. Could anyone please help. Thanks.

like image 424
Jeris Avatar asked Feb 19 '13 09:02

Jeris


People also ask

What is the regular expression for identifier?

identifier = letter (letter | digit)* real-numeral = digit digit* .

How is the C language identifier represented by a regular expression?

A regular expression is a sequence of characters that is used to search pattern. It is mainly used for pattern matching with strings, or string matching, etc. They are a generalized way to match patterns with sequences of characters. It is used in every programming language like C++, Java, and Python.

How do you represent special characters in regex?

The backslash in combination with a literal character can create a regex token with a special meaning. E.g. \d is a shorthand that matches a single digit from 0 to 9. Escaping a single metacharacter with a backslash works in all regular expression flavors.

What does ?= Mean in regex?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).


1 Answers

The regular expression you are looking for is:

[_a-zA-Z][_a-zA-Z0-9]{0,30} 

It will match an underscore or letter following by X underscores, letters or numbers, where 0 <= X <= 30

like image 112
Oscar Mederos Avatar answered Sep 21 '22 13:09

Oscar Mederos