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
5
votes
1 answer

Is the language {0^n 1^n 0^k | k != n} context free?

I believe this language isn't context free, because there is no chance that a PDA could compare 2 blocks of 0's and 1's of the same length and also remember it's length for later use. Unfortunately, I have no idea how to prove it. I tried using the…
5
votes
1 answer

drawing minmal DFA for the given regular expression

What is the direct and easy approach to draw minimal DFA, that accepts the same language as of given Regular Expression(RE). I know it can be done by: Regex ---to----► NFA ---to-----► DFA ---to-----► minimized DFA But is there any shortcut way?…
Niraj Rana
  • 83
  • 1
  • 1
  • 4
5
votes
1 answer

Pumping lemma (Regular language)

I need some help with a pumping lemma problem. L = { {a,b,c}* | #a(L) < #b(L) < #c(L) } This is what I got so far: y = uvw is the string from the pumping lemma. I let y = abbc^n, n is the length from the pumping lemma. y is in L because the number…
mrjasmin
  • 1,230
  • 6
  • 21
  • 37
5
votes
5 answers

regular expressions for url parser

I want to check if the string have the word user follows by number that can be 8 symbols max.
Marian Petrov
  • 625
  • 2
  • 9
  • 21
5
votes
1 answer

How to implement regular expression assertions/lookaround (i.e. \b style word boundary) using a DFA regular expression matcher

I would like to implement "word boundary" matches within a DFA based regular expression matcher. Can someone tell me how this is done? To give some background, I'm currently using the "dk.brics.automaton" library, but it does not support assertions…
user1255384
  • 303
  • 2
  • 8
4
votes
3 answers

Difference between a regular language and a regular grammar

My book gives similar but slightly different explanations of regular grammar and regular language. I doubt it's wrong, is a regular language the same thing of a regular grammar? The definition of my book is: A grammar is regular if all the…
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
4
votes
2 answers

Is (a^p )(b^q) a regular language

I read somewhere that {(a^p)(b^q):p,Q belong to N} is a regular language. However, i dont think this is correct. This can be proved using pumping lemma. Just want to verify if my solution is correct Let y be ab . Thus, x(y^n)z does not belong to L…
Programmer
  • 6,565
  • 25
  • 78
  • 125
4
votes
3 answers

Why is the complement of a regular language still a regular language?

According to my textbook, the complement of L1 = A* - L1 is a regular language as long as L1 is a regular language. Doesn't A* also include Context Free languages, Context Sensitive languages, and Recursively Enumerable languages? A*-L1 would…
4
votes
2 answers

How is it possible to split a string by whitespaces, and also keep commas separate?

I would like to break up a sentence into words. If it only contains whitespaces, then .split(/\s+/) works. But how is it possible to split by comma as well, and also keep the comma in the resulting array? I tried something like this, but it does not…
Iter Ator
  • 8,226
  • 20
  • 73
  • 164
4
votes
1 answer

Can regex be used to recognize any Context Free Language?

I know that regex packages can recognize a wider set of languages than just Regular Languages, but the use of recursive regex in Python regex to find arithmetic expressions in text strings makes me wonder if it is possible to recognize any Context…
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
4
votes
2 answers

How many languages does a DFA recognize?

According to Sipser's "Introduction to the Theory of Computation": If A is the set of all strings that machine M accepts, we say that A is the language of machine M and write L(M) = A. We say that M recognizes A ... A machine may accept several…
4
votes
1 answer

syntax error near unexpected token `(' in bash

I want to use grep and a regular expression to search a text document. When I type in this: grep -o ((D|d)ie|(D|d)as|(D|d)e(r|n|m|s)|(ei|Ei)(n|ne|nen|nem|ner|nes)) [A-ZÄÖÜ][A-Za-zäöü]* document.txt I get this: -bash: syntax error near unexpected…
bogdan
  • 97
  • 1
  • 7
4
votes
2 answers

Match product dimensions with regular expression

I am trying to match length width and height with a regular expression. I have the following cases Artikelgewicht3,7 Kg Produktabmessungen60,4 x 46,5 x 42 cm or Artikelgewicht3,7 Kg Produktabmessungen60 x 46 x 42 or Artikelgewicht3,7…
Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
4
votes
1 answer

Minimum pumping length for a regular language

How to calculate minimum pumping length of a regular language. For example if i have 0001* then minimum pumping length for this should be 4 ,that is 000 could not be pumped . Why it is so?
4
votes
2 answers

Preloading all images using $ImageCacheFactory

I have created a mobile application using ionic framework.It contains many images.I need to load all the images with out flickering.So i used $ImageCacheFactory for preloading all the images by refering this blog. I used below code.The problem is…
Muhsin Keloth
  • 7,855
  • 7
  • 39
  • 59