Regex for given languages with constraints

In summary, a regular expression is a sequence of characters used to define a search pattern and manipulate strings of text based on certain constraints and rules. To use regex for a given language, one must understand the syntax and rules of the language and construct a regular expression accordingly. Common constraints when using regex for a language include syntax, grammar, special characters, and specific patterns or formats. Yes, regex can be used to validate user input in a specific language by creating a regular expression that follows the rules of the language. There are tools and resources available for learning and using regex for different languages, such as online tutorials and regex testers.
  • #1
twoski
181
2

Homework Statement



Make a regex for the following languages on Σ = {a, b}:
(a) {w | each 'a' in w is immediately preceded and followed by 'b'}.
(b) {w | w has both 'ab' and 'ba' as sub-strings }
(c) {w ∈ {a, b} |(na(w) − nb(w)) mod 3 =/= 0}.

For c, the number of a's minus the number of b's, modulo 3, must not be 0.

The Attempt at a Solution


[/B]
a) (b*ba)(b*ba)*bb*+b*
b) (a+b)*((bab)+(aba))(a+b)*
c) (a*b*a)*+(a*b*aa)*+(a*b*b)*+(a*b*bb)*

C seems wrong and maybe i messed up B.. Any thoughts?
 
Physics news on Phys.org
  • #2


For a) and b), your regex expressions look correct.

For c), your regex is not quite correct. The correct regex for this language would be:

(a*b*a*b*a* + a*b*aa*b* + a*b*b*a* + a*b*bb*b*)*

This regex covers all possible combinations of a's and b's where the number of a's minus the number of b's, modulo 3, is not 0.
 

1. What is a regular expression?

A regular expression, also known as regex, is a sequence of characters that define a search pattern. It is used to find and manipulate strings of text based on certain constraints and rules.

2. How do I use regex for a given language?

To use regex for a given language, you need to first understand the syntax and rules of the language. Then, you can construct a regular expression that follows these rules and use it to search, validate, or manipulate strings in that language.

3. What are the common constraints when using regex for a language?

The common constraints when using regex for a language include the syntax and grammar of the language, as well as special characters, symbols, and keywords used in that language. Other constraints may include specific patterns or formats that the language follows.

4. Can I use regex to validate user input in a specific language?

Yes, regex can be used to validate user input in a specific language. By creating a regular expression that follows the constraints and rules of the language, you can ensure that the user's input matches the expected format.

5. Are there any tools or resources for learning and using regex for different languages?

Yes, there are many tools and resources available for learning and using regex for different languages. These include online tutorials, documentation, and online regex testers that allow you to practice and test your regular expressions in real-time.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Precalculus Mathematics Homework Help
Replies
11
Views
2K
  • Math Proof Training and Practice
4
Replies
114
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Differential Equations
Replies
3
Views
2K
Back
Top