Frank
Frank

Reputation: 23

Regex: Everything before a word

I have the following text:

There are 12.800.500 sorts of animals.

Now, I want to get 12.800.800 as the output, when I search for "sorts". How could I do that? I tried

\d+(?= sorts)

but this gives me only "500" as a result and not the whole number with dots. How can I make sure I get "everything" before "sorts" until the first space?

Thanks!

Upvotes: 0

Views: 140

Answers (1)

anubhava
anubhava

Reputation: 786041

The text you're trying to match also includes DOTS not just digits so use this regex:

\b[\d.]+(?=\s+sorts)

RegEx Demo

[\d.] will match either a digit or a DOT.

To match any non-space character use:

\S+(?=\s+sorts)

Upvotes: 1

Related Questions