Week 5 - C Regex & C Fundamentals Part 3
Regex in C
Compiling Regex
Agenda: Overview of regex compilation in C.
Basic Patterns:
Phone Number Format:
###-###-####Regex:
^\d{3}-\d{3}-\d{4}$
Date Format:
MM/DD/YYYYRegex:
^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$
Password Criteria: At least one upper case, one lower case, one digit
Regex:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$
Regular Expression Engines
Definition: Software that processes regex patterns.
Common Engines: Perl, PHP, Java, JavaScript, Python, R, POSIX.
C Implementation: Uses POSIX regex syntax, compatible with UNIX tools (grep, egrep, awk, sed).
Regex Functions in C
Library Used:
<regex.h>Functions:
regcomp()Compiles the regex pattern.
regexec()Executes the compiled pattern against a string.
Compilation Command in gcc:
gcc my_prog.c -o my_prog -lregex
Compiling and Executing Regex
regcomp():Takes 3 arguments: regex variable, pattern, and special flags (e.g.,
REG_EXTENDED).
regexec():Takes 5 arguments: compiled regex, string, number of sub-expressions, and offset array.
Example
Valid Email Check:
Regex:
^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5})$
C Fundamentals
Expressions
Arithmetic Operators: +, -, *, /, %, ++, --
Relational Operators: <, >, <=, >=, != (returns 0 for false, 1 for true in C)
Conditional Operators:
&&,||,?Assignment Operators: =, +=, -=, *=, /=, &=, |=, ^=
Unary Operators: * (pointer dereference), & (address), sizeof (size of type), -> (field selection)
Bitwise Operators:
And (&), Or (|), Exclusive Or (^), Left Shift (<<), Right Shift (>>), Complement (~)
Scope Rules
Local Variables:
Declared in functions, lifetime = function execution, scope = function body.
Global Variables:
Declared outside functions, lifetime = program duration, scope = entire file.
Static Variables:
Use
staticprefix to limit scope; persistent storage upon function exit.
Example of Scope
#include <stdio.h>
double pi = 3.14159; // Global variable
int main(void) {
char user_input[30]; // Local variable
scanf("%s", user_input); // Read user input
}Next Class
Topic: File I/O in C