Use Cases (अपडेट: 2/6/2026)

Claude Code से regex बनाना, टेस्ट करना और रिव्यू करना

Claude Code से email, phone और log regex बनाएं, named capture, node:test और review checklist के साथ.

Claude Code से regex बनाना, टेस्ट करना और रिव्यू करना

Regular expression, यानी regex, तब सबसे ज्यादा टूटता है जब requirement साफ नहीं होती। कोई pattern user@example.com को सही माने, लेकिन user+tag@sub.example.co.jp को गलत कर दे, या गलती से user@.com को पास कर दे। समस्या सिर्फ syntax याद रखने की नहीं है; असली समस्या यह है कि हमें पहले से तय करना होता है कि कौन सा text स्वीकार होगा और कौन सा नहीं।

Claude Code को regex याद कराने वाले shortcut की तरह नहीं, बल्कि एक review साथी की तरह इस्तेमाल करें। आप उसे allowed examples, rejected examples, regex, executable tests और review checklist एक साथ बनवाने के लिए कह सकते हैं। Regex text की shape बताने वाली छोटी भाषा है। Named capture का मतलब है निकाले गए हिस्से को नाम देना, जैसे timestamp या requestId, ताकि code पढ़ते समय match[4] का अंदाजा न लगाना पड़े।

अगर आप Claude Code में नए हैं, तो पहले Claude Code getting started guide पढ़ें। बेहतर prompt लिखने के लिए 5 tips for better prompts भी उपयोगी है। Official reference के लिए Claude Code overview और JavaScript regex के लिए MDN Regular expressions देखें।

काम करने का flow

flowchart LR
  A["Allowed examples"] --> C["Claude Code को prompt"]
  B["Rejected examples"] --> C
  C --> D["Regex और helper functions"]
  D --> E["Node.js tests"]
  E --> F["Review और pitfalls"]

सिर्फ “email regex बना दो” कहना काफी नहीं है। बताएं कि काम validation का है, extraction का, replacement का या log analysis का। यही examples बाद में tests का contract बनते हैं।

पहला prompt

Email addresses, Japanese phone numbers और application logs के लिए JavaScript regex helper बनाएं।

Requirements:
- Node.js में सीधे चले
- user+tag@sub.example.co.jp allow हो
- user..name@example.com और user@.com reject हों
- 090-1234-5678, 03-1234-5678, 05012345678 allow हों
- logs से timestamp, level, service, requestId और message named captures से निकालें
- node:test के tests जोड़ें
- जहां business validation regex से नहीं करनी चाहिए, वहां explanation दें

Scope तय करना जरूरी है। Email validation को पूरी RFC specification तक ले जाना आम sign-up form के लिए ज्यादा हो सकता है। बेहतर है कि regex obvious mistakes रोके और deliverability confirmation email या backend logic संभाले।

Example 1: Email, phone और log helper

इसे regex-helper.mjs के रूप में save करें और node regex-helper.mjs चलाएं।

import { fileURLToPath } from "node:url";

export const emailRegex =
  /^(?!.*\.\.)[A-Z0-9_%+-]+(?:\.[A-Z0-9_%+-]+)*@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,63}$/i;

export const emailSearchRegex =
  /[A-Z0-9_%+-]+(?:\.[A-Z0-9_%+-]+)*@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,63}/gi;

export const normalizedJapanesePhoneRegex = /^0(?:[5789]0\d{8}|[1-9]\d{8,9})$/;

export const looseJapanesePhoneSearchRegex =
  /0\d{1,4}[-\s]?\d{1,4}[-\s]?\d{3,4}/g;

export const appLogRegex =
  /^\[(?<timestamp>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z)\]\s+(?<level>INFO|WARN|ERROR)\s+(?<service>[a-z][a-z0-9-]*)\s+requestId=(?<requestId>[A-Za-z0-9_-]+)\s+message="(?<message>[^"]*)"$/;

export function isEmail(input) {
  return emailRegex.test(input.trim());
}

export function normalizePhone(input) {
  return input.replace(/[()\s-]/g, "");
}

export function isJapanesePhone(input) {
  return normalizedJapanesePhoneRegex.test(normalizePhone(input));
}

export function extractContacts(text) {
  const emails = [...text.matchAll(emailSearchRegex)]
    .map((match) => match[0])
    .filter(isEmail);

  const phones = (text.match(looseJapanesePhoneSearchRegex) ?? []).filter(
    isJapanesePhone,
  );

  return {
    emails: [...new Set(emails)],
    phones: [...new Set(phones)],
  };
}

export function parseLogLine(line) {
  const match = line.match(appLogRegex);
  if (!match?.groups) return null;

  return {
    timestamp: match.groups.timestamp,
    level: match.groups.level,
    service: match.groups.service,
    requestId: match.groups.requestId,
    message: match.groups.message,
  };
}

if (process.argv[1] === fileURLToPath(import.meta.url)) {
  const text = "Contact: user+tag@sub.example.co.jp / 090-1234-5678";
  const log =
    '[2026-06-02T10:15:30.000Z] ERROR billing-api requestId=req_123 message="payment failed"';

  console.log(extractContacts(text));
  console.log(parseLogLine(log));
}

Phone number को validate करने से पहले normalize किया गया है। इसका मतलब है कि hyphen और spaces हटाकर comparison आसान बनाया जाता है। Logs में named captures इस्तेमाल किए गए हैं, इसलिए match.groups.requestId पढ़ते ही पता चलता है कि कौन सा data निकला है।

Example 2: Behavior lock करने वाले tests

इसे regex-helper.test.mjs के रूप में save करें और node --test regex-helper.test.mjs चलाएं।

import test from "node:test";
import assert from "node:assert/strict";
import {
  extractContacts,
  isEmail,
  isJapanesePhone,
  parseLogLine,
} from "./regex-helper.mjs";

test("validates practical email addresses", () => {
  assert.equal(isEmail("user@example.com"), true);
  assert.equal(isEmail("user+tag@sub.example.co.jp"), true);
  assert.equal(isEmail("user..name@example.com"), false);
  assert.equal(isEmail("user@.com"), false);
  assert.equal(isEmail("@example.com"), false);
});

test("validates Japanese phone numbers after normalization", () => {
  assert.equal(isJapanesePhone("090-1234-5678"), true);
  assert.equal(isJapanesePhone("03-1234-5678"), true);
  assert.equal(isJapanesePhone("05012345678"), true);
  assert.equal(isJapanesePhone("123-4567-8901"), false);
  assert.equal(isJapanesePhone("090-123-456"), false);
});

test("extracts contacts from free text", () => {
  assert.deepEqual(
    extractContacts("support: user+tag@example.com, tel: 090-1234-5678"),
    {
      emails: ["user+tag@example.com"],
      phones: ["090-1234-5678"],
    },
  );
});

test("parses application logs with named captures", () => {
  const parsed = parseLogLine(
    '[2026-06-02T10:15:30.000Z] WARN auth-service requestId=req_abc message="retry required"',
  );

  assert.deepEqual(parsed, {
    timestamp: "2026-06-02T10:15:30.000Z",
    level: "WARN",
    service: "auth-service",
    requestId: "req_abc",
    message: "retry required",
  });
});

Claude Code से test run भी करवाएं।

node --test regex-helper.test.mjs चलाएं।
अगर test fail हो, तो पहले बताएं कि regex गलत है या test data।
Email support बढ़ाने से पहले एक allowed और एक rejected example जोड़ें।

Example 3: Log extraction prompt

Logs regex के लिए अच्छे होते हैं, क्योंकि format application control करता है।

logs/app.log पढ़ें, सिर्फ ERROR lines निकालें, और requestId तथा message को CSV में लिखें।
regex-helper.mjs वाला appLogRegex इस्तेमाल करें।
जो lines parse नहीं हों, उन्हें चुपचाप न छोड़ें; अंत में count दिखाएं।
CSV में email addresses या phone numbers न लिखें।

Real logs में old format, truncated lines और temporary debug output मिलते हैं। null return करने वाला parser silent ignore करने वाले script से ज्यादा reviewable होता है।

Review template

## Regex review request

Files:
- regex-helper.mjs
- regex-helper.test.mjs

Review:
- Are allowed and rejected examples covered by tests?
- Are email, phone, and log responsibilities separated?
- Are named capture names readable?
- Is there any ambiguous repetition that could cause ReDoS?
- Could personal data leak into logs or CSV output?

Output:
- For each issue, include file, line, reason, and suggested fix
- Ask a question instead of guessing when the business rule is unclear
- Run node --test regex-helper.test.mjs after changes

ReDoS का मतलब है ऐसा regex जो खास input पर बहुत ज्यादा समय ले सकता है। अगर pattern में nested repetition है, तो Claude Code से इसे अलग से review करने को कहें।

आम pitfalls

Pitfallबेहतर instruction
सिर्फ valid examples देनाकम से कम 3 rejected examples जोड़ना
हर जगह .* लगानासाफ boundary जैसे [^"]* इस्तेमाल करना
g flag वाली regex को test() में दोहरानाvalidation regex और search regex अलग रखना
capture index पर निर्भर रहनाnamed capture या non-capturing group इस्तेमाल करना
regex को business validation समझनाdeliverability और existence backend में जांचना

Regex input shape ठीक करने और controlled text से data निकालने के लिए अच्छा है। यह साबित नहीं कर सकता कि email सच में deliver होगा या phone number active है।

CTA

Regex सुधार revenue path से जुड़ा हो सकता है: lead forms, payment logs, support intake और download signup। अभ्यास के लिए free Claude Code cheatsheet से शुरू करें। Reusable prompts और templates के लिए products देखें। Team rollout में validation, log review और review gates चाहिए हों तो Claude Code training and consultation बेहतर रास्ता है।

निष्कर्ष

Claude Code से regex बनवाते समय examples, counterexamples, tests और review criteria साथ दें। Masa के परीक्षण में “regex और tests साथ बनाओ” कहना “सिर्फ regex लिखो” से ज्यादा स्थिर निकला। इससे user+tag email, hyphen वाले phone numbers और named log captures जैसे cases पहले ही सुरक्षित हो गए।

#Claude Code #regular expressions #regex #debugging #testing
मुफ़्त

मुफ़्त PDF: Claude Code cheatsheet

Email डालें और commands, review habits तथा safe workflow वाली एक-page PDF पाएँ.

हम आपका data सुरक्षित रखते हैं और spam नहीं भेजते.

Masa

लेखक के बारे में

Masa

Claude Code workflow और team adoption पर काम करने वाला engineer.