How do I find a regular expression for a particular language?
Occasionally you can just stare at the language, get insight, and write down a regular expression just like that.  However, more typically, we need a systematic procedure.  Fortunately, the field of formal languages provides a more systematic approach that will help you structure your approach to this problem.  Often, one effective approach is as follows:

Step 1. Find a non-deterministic finite-state automaton (NFA) for the language.

Step 2. Convert the NFA to a regular expression.

Step 3. Double-check whether your regular expression is correct.

In more detail, here is how you do each of those two steps:

Step 1. To find a NFA for your language, you can think of this as a programming problem, where you have to write a program that works in a very limited programming language where you are only allowed to have a fixed, finite amount of state.  Try to write a program that reads in a string one letter at a time and decides whether the input string belongs to the language or not, using only a fixed amount of memory (say, $c$ memory cells, where $c$ is some constant that does not depend on the input string, not even on the length of the input string).  This program then corresponds naturally to a NFA, where the NFA has one state per possible value of the program's memory.

Sometimes, it can also be helpful to use the closure properties of NFA.  For instance, if the language $L$ is specifies as $L = L_1 \cup L_2$, then it suffices to build a NFA for $L_1$ and a NFA for $L_2$; then the closure properties for regular languages allow you to derive a NFA for $L$.  For more techniques to build a NFA, take a look at our reference question How to prove a language is regular? for more approaches.

Step 2. To convert the NFA to a regular expression, apply the algorithm described in How to convert finite automata to regular expressions?.

Step 3. To check whether your regular expression seems to be correct, you can try testing it on a few example strings to see if it seems to give the correct answer.  Try both several examples that are in the language and several that are not.



This is just one approach.  There are other approaches, including writing down a regular grammar and converting it to a regular expression, or writing a system of linear expressions in regular languages and converting to a regular expression using Arden's Lemma, or others.  For an overview of those methods, take a look at How to prove a language is regular?, How to convert finite automata to regular expressions?, and Known algorithms to go from a DFA to a regular expression.

You can also look at other examples of this kind of problem on this site, e.g., by looking at questions tagged regular-expressions.