Reputation: 5365
I am trying to craft a regular expression that will match all characters after (but not including) the first space in a string.
Input text:
foo bar bacon
Desired match:
bar bacon
The closest thing I've found so far is:
\s(.*)
However, this matches the first space in addition to "bar bacon", which is undesirable. Any help is appreciated.
Upvotes: 2
Views: 9650
Reputation: 93676
You don't need look behind.
my $str = 'now is the time';
# Non-greedily match up to the first space, and then get everything after in a group.
$str =~ /^.*? +(.+)/;
my $right_of_space = $1; # Keep what is in the group in parens
print "[$right_of_space]\n";
Upvotes: 2
Reputation: 75488
I'd prefer to use [[:blank:]]
for it as it doesn't match newlines just in case we're targetting mutli's. And it's also compatible to those not supporting \s
.
(?<=[[:blank:]]).*
Upvotes: 3
Reputation: 32797
You can also try this
(?s)(?<=\S*\s+).*
or
(?s)\S*\s+(.*)//group 1 has your match
With (?s)
.
would also match newlines
Upvotes: 1
Reputation: 129507
You can use a positive lookbehind:
(?<=\s).*
(demo)
Although it looks like you've already put a capturing group around .*
in your current regex, so you could just try grabbing that.
Upvotes: 5