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
14
votes
2 answers

Is L = {a^n b^m | n>m} a regular or irregular language?

How can I prove if L = {a^n b^m | n>m} is a regular or irregular language?
Jerald James Capao
  • 175
  • 1
  • 1
  • 7
14
votes
4 answers

To make sure: Pumping lemma for infinite regular languages only?

So this is not about the pumping lemma and how it works, it's about a pre-condition. Everywhere in the net you can read, that regular languages must pass the pumping lemma, but noweher anybody talks about finite languages, which actually are a part…
13
votes
2 answers

Minimum pumping length for the following regular languages

What are the minimum pumping length for the following languages ? The empty language (01)* 10(11*0)*0 1011 011 U 0*1* Here are my solutions. Please correct me if I'm wrong. p = 0 because the language has no pumpable strings p = 2 because 01 is…
12
votes
6 answers

How should one proceed to prove (or find) if two regular expressions are same or equivalent?

For example, in an assignment given to me, we were asked to find out if two regular expressions are equal or not. (a+b+c)* and ((ab)**c*)* My question is how is one supposed to do that? If I draw the transition graphs for both and then run a few…
11
votes
4 answers

An infinite language can't be regular? What is a finite language?

I read this in a book on computability: (Kleene's Theorem) A language is regular if and only if it can be obtained from finite languages by applying the three operations union, concatenation, repetition a finite number of times. I am…
10
votes
2 answers

What is the power of regular expressions?

As the name suggests we may think that regular expressions can match regular languages only. But regular expressions we use in practice contain stuff that I am not sure it's possible to implement with their theoretical counterparts. How for example…
Artium
  • 5,147
  • 8
  • 39
  • 60
9
votes
2 answers

Which programming languages have a regular grammar?

I'm curious about which (if any) real-world programming languages have a regular grammar (i.e. the set of all syntactically correct programs is regular). See also this question: What programming languages are context-free?.
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
9
votes
2 answers

How to use back-reference of sed replacement command correctly considering a special Regular Expression

I am learning the sed s/regexp/replacement/ command on linux. There are some numbers from phone.txt (555)555-1212 (555)555-1213 (555)555-1214 (666)555-1215 (777)555-1217 I'd like to use the regular expression (which I have tested on…
David17
  • 173
  • 1
  • 1
  • 9
9
votes
2 answers

Regular languages vs. non-regular ones

Can anyone kindly help me distinguish between regular languages (i.e. those that can be described by regular expressions) and other languages that are not regular in terms of the formal definition of regular languages? Furthermore, can you provide…
Daniel
  • 1,484
  • 5
  • 24
  • 42
9
votes
5 answers

Is a*b* regular?

I know anbn for n > 0 is not regular by the pumping lemma but I would imagine a*b* to be regular since both a,b don't have to be the same length. Is there a proof for it being regular or not?
Jasoneer
  • 2,078
  • 2
  • 15
  • 20
9
votes
1 answer

If we know a CFG only generates regular language, can we get the corresponding regular expression?

As we know, given a regular grammar, we have algorithm to get its regular expression. But if the given grammar is context-free grammar (but it only generates regular language), like S->aAb A->bB B->cB|d Is there any existing algorithm that…
JackWM
  • 10,085
  • 22
  • 65
  • 92
8
votes
3 answers

Ignore everything in a directory except one subfolder

I have a directory ~/x7/music/sfx. There are some files and folders in the root of ~/x7/music. I need to sync only the sfx folder and ignore anything else in music. I've tried many variants, but all of them was wrong. ignore = Name…
det
  • 81
  • 1
  • 3
8
votes
3 answers

substring match faster with regular expression?

After having read up on RE/NFA and DFA, it seems that finding a substring within a string might actually be asymptotically faster using an RE rather than a brute force O(mn) find. My reasoning is that a DFA would actually maintain state and avoid…
dhruvbird
  • 6,061
  • 6
  • 34
  • 39
8
votes
1 answer

Can a regular expression itself be parsed with a regular expression?

I am reading the code of a regular expression parser, and start to wonder if the syntax of regular expression is itself regular, and can be expressed with another (quite complicated) regular expression? rere = "" # the regular expression of regular…
NeoWang
  • 17,361
  • 24
  • 78
  • 126
8
votes
3 answers

Need Regular Expression for Finite Automata: Even number of 1s and Even number of 0s

My problem may sounds different to you. I am a beginner and I am learning Finite Automata. I am googing over Internet to find the Regular Expression for Finite Automata of Given Machine Below. Can anyone help me to write "Regular Expression…
jyoti
  • 350
  • 1
  • 5
  • 15
1
2
3
60 61