Regular Expressions for Strings with Specific Character Patterns

In summary, the conversation discusses finding regular expressions for different types of strings using the alphabet {a, b, c}. The first expression covers strings with no more than 3 a's, the second covers strings with a number of a's divisible by 3, and the third covers strings with the substring "aaa" occurring only once. The conversation also includes a discussion about a regular expression for strings containing at least one of each letter in the alphabet. Both the proposed solution (a(bUc)*Ub(aUc)*Uc(aUb)*)* and the book's solution (aUbUc)*c(aUbUc)*b(aUbUc)*a(aUbUc)* are correct.
  • #1
kliker
104
0

Homework Statement


Σ = {a,b}
find regular expression for

1)all strings that haven't got more than 3 a's
2)all strings that have a number of a's that is divisible by 3
3)all strings that have the substring aaa only one time

The Attempt at a Solution


1) b*Ub*ab*Ub*ab*ab*Ub*ab*ab*ab*

2) (b*Ub*ab*ab*ab*)*

3) b*(aUaaUe)b*aaab*(aUaaUe)b*

can you please tell me if I am correct? this is not homework actually but i didnt know where to post it, I am just preparing myself for the upcoming exams and need to know if i have these correct

thanks in advance
 
Physics news on Phys.org
  • #2
i have one more

Σ = {a,b,c}

i want a regular expression for all strings that contain at least 1 a, 1 b and 1 c

ok my solution is this

(a(bUc)*Ub(aUc)*Uc(aUb)*)*

but the book has this solution

(aUbUc)*c(aUbUc)*b(aUbUc)*a(aUbUc)*

which is right but is my solution wrong?
 

Related to Regular Expressions for Strings with Specific Character Patterns

1. What are regular expressions?

Regular expressions, also known as regex, are a sequence of characters that define a search pattern. They are used to find and manipulate text in strings, such as in programming languages or text editors.

2. How can regular expressions help me?

Regular expressions can help you in a variety of tasks, such as finding and replacing specific patterns of text, validating input, and extracting information from larger strings of text. They can also save you time and effort by allowing you to perform complex search and replace operations with just a few lines of code.

3. Are regular expressions difficult to learn?

Like any new concept, regular expressions may seem challenging at first, but with practice and patience, they can become a powerful tool in your toolkit. There are many online resources and tutorials available to help you learn and master regular expressions.

4. Can regular expressions work with any programming language?

Yes, regular expressions are supported by most programming languages, including but not limited to JavaScript, Python, Java, and C++. Each language may have slight variations in syntax, but the basic principles of regular expressions remain the same.

5. Are there any limitations to using regular expressions?

While regular expressions can be very versatile, they may not be the best solution for every problem. For example, they may not be the most efficient way to process large amounts of data. It's important to consider the complexity and size of your data set before using regular expressions.

Similar threads

  • Calculus and Beyond Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Calculus and Beyond Homework Help
Replies
20
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
8K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
3K
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Introductory Physics Homework Help
Replies
9
Views
1K
Replies
12
Views
477
Back
Top