java - Explicitly defining the end of the input in a regular expression using $ -



java - Explicitly defining the end of the input in a regular expression using $ -

i have code using regular look separate input string 2 words, sec word optional (i know might utilize string.split() in particular case, actual regular look bit more complex):

package com.example; import java.util.regex.matcher; import java.util.regex.pattern; public class dollar { public static void main(string[] args) { pattern pattern = pattern.compile("(.*?)\\s*(?: (.*))?$"); // works //pattern pattern = pattern.compile("(.*?)\\s*(?: (.*))?"); // not work matcher matcher = pattern.matcher("first second"); matcher.find(); system.out.println("first : " + matcher.group(1)); system.out.println("second: " + matcher.group(2)); } }

with code, expected output

first : first second: sec

and works if sec word not there.

however, if utilize other regexp (without dollar sign @ end), empty strings / nulls capture groups.

my question is: why have explicitly set dollar sign @ end of regexp match "the end of input sequence" (as javadoc says)? in other words, why end of regular look not implicitly treated end of input sequence?

that due lazy nature of regex finds & captures many empty matches.

if utilize improve regex:

(\s+)(?: (.*))?

then work with:

(\s+)(?: (.*))?$

java regex

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -