Chuyển đến nội dung chính
eLearner.app
Mô-đun 6 · Bài học 4 trong tổng số 424/32 trong khóa học~12 min
Bài học theo mô-đun (4/4)

Nhìn lại trong thực tế

Let's put all four forms of lookaround together in real-world scenarios. Lookarounds shine when you need to:

  1. Extract a value without the context that identifies it.
  2. Validate a string against multiple independent conditions.
  3. Filter matches that satisfy some conditions but not others.

Password validation

Code
Pattern: ^(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z\d]).{8,}$
Checks:
  - at least one uppercase     (?=.*[A-Z])
  - at least one digit         (?=.*\d)
  - at least one symbol        (?=.*[^a-zA-Z\d])
  - minimum length 8           .{8,}

Each lookahead checks one condition, starting from the beginning (^). They are all zero-width: the engine stays at position 0 and then consumes with .{8,}$.

Extraction between delimiters

Code
Pattern: (?<=\().+?(?=\))
Sample:  Funzione foo(bar) e baz(qux, qix)
Match: "bar", "quux, qix" (without parentheses)

Lookbehind + lookahead + lazy quantifier: extracts the content inside the parentheses without including them.

Combining lookahead and lookbehind

Using lookahead and lookbehind together allows isolating strings that lie within specific tags or formats (e.g. extracting text between two tags without including the tags in the final match). This avoids subsequent string cleanup operations.

Try it

tập thể dục#regex.m6.l4.e1
Nỗ lực: 0Đang tải…

Extract the value of every `key=value` assignment from the log: ONLY the values (no keys, no `=`). Values may contain letters and digits.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Move `=` inside a lookbehind (?<==): this way the match contains only the value.

Giải pháp khả dụng sau 3 lần thử

Review exercise

tập thể dục#regex.m6.l4.e2
Nỗ lực: 0Đang tải…

Find which words have at least ONE uppercase letter AND at least ONE digit (in any order), locating them in the text.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Double lookahead at the start: (?=\\w*[A-Z])(?=\\w*\\d). Then consume the word with \\w+.

Giải pháp khả dụng sau 3 lần thử

Additional challenge

tập thể dục#regex.m6.l4.e3
Nỗ lực: 0Đang tải…

Extract only numerical digits that are enclosed exactly between parentheses, e.g. extract `102` from `(102)` without capturing the parentheses.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Combine a positive lookbehind (?<=\( ) for the open parenthesis and a positive lookahead (?=\) ) for the closed one.

Giải pháp khả dụng sau 3 lần thử