Questions tagged [regular-language]

Regular language is a language which can be represented by a regular expression and thus every string in the language can be accepted by the corresponding deterministic finite automaton. Note: Regular Language should not be confused with Regular Expressions. For question regarding pattern matching within strings, use the [regex] tag instead.

Given an alphabet (finite set of symbols) Σ, a language is a set of all sequences of such symbols in that alphabet. A language is a regular language exactly when it can be expressed in terms of a (formal) regular expression and the membership of any string can be decided by a finite-state machine.

Regular languages belong to the highest hierarchy of the Chomsky Hierarchy, and are also called Type-3 grammars. They are above the Type-2 context-free languages which are recognized by pushdown automata, which are above the Type-1 context-sensitive languages recognized by linear bounded automata, and above the Type-0 recursively enumerable languages which can be recognized by Turing Machines. All regular languages are context-free, context-sensitive, and recursively enumerable. Formal regular expressions can be converted to deterministic finite state machines and to non deterministic finite machines and still represent the same regular language.

Please do not confuse this with regex. Most regex engines are far more expressive than formal regular expressions, finite state machines, and can represent non-regular languages.

Construction of a Regular Language

The set of all regular languages over a given alphabet Σ can be produced exactly by this process:

  • The empty language {}, rejecting all strings.
  • The language containing only the empty string ε
  • All languages containing only a single symbol s ∈ Σ.
  • Every language created by the union, concatenation, or kleene-star of regular languages. Suppose v and w are strings of a regular language A and B respectively:
    • The union (v|w) is also regular. It accepts languages that are in any of A or B.
    • The concatenation vw is also regular.
    • The kleene-star v* is also regular. It means any copies of strings in A concatenated, including 0.

Examples and Nonexamples of Regular Languages

  • Given a simple alphabet Σ = {0, 1}, where | represents union, * represents kleene-star, these formal regular expressions all represent represents a regular language:

    • The regular expression "0", "1", "(0|1)", "01", "11", "0*" are all regular.
    • The regular expression "(0(0|1)*1)", representing all binary strings beginning with 0 and ending with 1, is regular.
    • Given a regular expression R, the language "R+" and "R?" all represent a regular language, whereas + represents one or more, and ? represents zero or one. Namely, "R+" is equivalent to "RR*", and "R?" is equivalent to "(R|ε)".
    • Given a regular expression R, the language "R{m,n}" is regular for all natural m,n, where {m,n} represents "from m copies to n copies". This is because it also involves union and concatenation: "R{1,3}" is expanded to "(R|RR|RRR)".
  • Given an alphabet used by regex engines, usually an ASCII or Unicode alphabet containing all ASCII or Unicode characters respectively:

    • The regex /^.+$/ is regular. It includes all non-empty sequences of any character.
    • The regex /^#[A-Za-z]{1,3}[0-9]{2,4}$/ represents a regular language, consisting all strings which being with a hashtag, then one to three ASCII letters, followed by two to four decimal digits.
    • The regex /^([\d][\w])*$/ represents a regular language. It consists all strings which alternate digit characters and word characters. The shorthand \d and \w are examples of union.
  • Many regex engines are much more expressive than regular languages. Backreferences can cause a regex to represent a non-regular language, and consequently they cannot be decided by a finite state machine.

    • The regex "(.+)\1" represents an irregular language. Involving a backreference capturing the first group .+, it accepts all the sequences of uppercase Latin letters repeated exactly twice. They are called squares in formal language theory.
      • "ABCABC", "1234.1234." are accepted
      • "ABCAB", "1234567891234567890" are rejected.

Further Reading

914 questions
-3
votes
1 answer

Replace all occurrences of string only if they don't start by '@'

I would like to replace all the occurrences of a string, as long as those don't start by '@', so for example in the following query: (surname = @surname and surname = @surname1) if I want to replace surname, it will only replace the two of them…
Rafael
  • 1,099
  • 5
  • 23
  • 47
-3
votes
1 answer

Find a regular expression for strings containing the substring 01a and even number of 1's over the alphabet {0,1,a}

Regular language given by its description: The set of all strings of {0,1, a}, which contain the substring '01a' and an even number of '1'. For example, '01a1 ', '101a', '101a101'. How to construct a regular expression that specifies the language?
-4
votes
2 answers

using regular expression for preg replace

i wanna to know the best regular expression to replace this string with the vid_id in it $code = '<object width="420" height="345">
<param name="movie"…
-4
votes
1 answer

How to replace single and double digit numbers with single character in js?

I have an essay as a big string with letters and numbers (as page numbers) (only single digit and double digits). I want all of them to be replaced by "#". I tried str.replace(/[0-9]/g,"#"), this worked for 0 to 9 numbers, but with the double…
-4
votes
2 answers

find first matches character in regular expression

i have a string that i want to find "a" character and first "c" character and replace with "*" character, but regular expression find all "a" and "c" character i don't know how do it. here my code: var pattern=/[a][c]/g; //here my pattern var…
user12026752
-4
votes
1 answer

Regular expression for no more than 1 zero in a row

I need to write regular expression for language {0,1} with no more than one zero in a row.
-4
votes
1 answer

How to search this kind of pattern in regex

This is the sample text I want to get the regex of the Match specified string that regex will qualify the Not match conditions 1abc.def.ghi (Match) abc.111.ghi (Match) 123.123.123.132.123.123…
Ajil Raju
  • 37
  • 1
  • 2
  • 9
-4
votes
1 answer

Grep a block of text using pattern

I have short text file, where i should make output of data using special pattern. My file: 99 test1 88 test2 10 test3 11 test1 12 test1 13 test2 14 test3 17 test1 18 test4 One by one, from test1 to test2 and to test3. So... I have written the…
Valeriu
  • 1
  • 1
-4
votes
1 answer

How is it possible that the language a^n b^2n is regular if and only if it was finite such that 100 => n <= 0?

how is it possible that the language a^n b^2n is regular if and only if it was finite such that 100 => n <= 0? I know that a language in such a form of ( a^n b^n ) when n=>0 is not regular, since we need a temporary memory to keep track of the…
-4
votes
1 answer

Regular expression to perform following operation

Input string contains multiple key[with some value], which we need to replace with key[with some value],val[value which is same as key]. Input string: ...key[102]...key[108]... key[211]... Output string: ...…
ITQuest
  • 113
  • 1
  • 2
  • 8
-4
votes
1 answer

Regular expression over a language - slight confusion

Alphabet: Σ = {0, 1} I'm not too sure what this question is asking. I've posted the answer (just so people don't think it's homework - I'm actually trying to learn these). I'm not quite sure why we have an | in the middle of both of these. I…
PaulEx10
  • 155
  • 2
  • 10
-5
votes
2 answers

create a regular expression of a specific sentence

I am using a constraint file for my system and basically i am using this line for parsing my values: angle(Vector(JointA,jointB),Vector(JointA,jointB),minValue,maxValue) Can you please help me and specify the regular expression of this line. I want…
-6
votes
1 answer

How to use re.findall to get the url string?

"foldGroup.registerImage({ domId: 'listimg7', srcUrl: 'https://ec.yimg.com/ec/?url=https%3A%2F%2Fd3vv6xw699rjh3.cloudfront.net%2F9f689b-1904037587_1_160.jpg&t=1460964135&ttl=43200&maxWidth=160&maxHeight=160&sig=QSY1BP0sCebMxqEN6irjXQ--~C' });" This…
mingxin zhao
  • 109
  • 9
-6
votes
1 answer

Any Language that can be generated LL1 Grammar is regular. True/False

I am reading about grammars, I have a question is every language generated by LL(1) grammar is regular? I know that every regular language can be generated by LL(1) grammar.
1 2 3
60
61