简体   繁体   中英

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM