Text Processingflags: m
Java Package Declaration
Match Java `package com.example.app;` declarations and capture the dotted package path.
Try it in RegexPro →Available in
Pattern
regexengine-agnostic
^\s*package\s+([a-z][\w]*(?:\.[a-z][\w]*)*)\s*; (flags: m)Raw source: ^\s*package\s+([a-z][\w]*(?:\.[a-z][\w]*)*)\s*;
How it works
^\s* allows leading whitespace at line start (m flag enables per-line). package\s+ requires the keyword. ([a-z][\w]*(?:\.[a-z][\w]*)*) captures the package path: each segment must start with a lowercase letter (Java convention) and continue with word characters. \s*; matches the required terminating semicolon.
Examples
Input
package com.example.app;Matches
package com.example.app;
Input
package io.vendor.module.sub;Matches
package io.vendor.module.sub;
Input
// no packageNo match
—Common use cases
- •Java source-file analysis
- •Migrating between package namespaces
- •Build-tool dependency resolution
- •Generating documentation from source layout
Related patterns
JavaScript Variable Declaration
Text ProcessingMatch JavaScript / TypeScript variable declarations (`var`, `let`, `const`), capturing the keyword and identifier name.
Markdown Link
Text ProcessingMatch Markdown links [link text](url) and capture both the display text and the URL.
Named Capture Group (Date)
Text ProcessingDemonstrate named capture groups by extracting year/month/day from ISO-style dates.
Base64 String
Text ProcessingMatch Base64-encoded strings, including proper padding with = and == characters.