Java Package Declaration
Match Java `package com.example.app;` declarations and capture the dotted package path.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("^\\s*package\\s+([a-z][\\w]*(?:\\.[a-z][\\w]*)*)\\s*;", "m");
const input = "package com.example.app;";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"^\s*package\s+([a-z][\w]*(?:\.[a-z][\w]*)*)\s*;", re.MULTILINE)
input_text = "package com.example.app;"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?m)^\s*package\s+([a-z][\w]*(?:\.[a-z][\w]*)*)\s*;`)
input := `package com.example.app;`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
^\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
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.