0

I want query Minecraft servers and get their version. But when I query servers In version I get strings:

BotFilter 1.8.x-1.12.x => I need get 1.12
BotFilter 1.8.x-1.12.x by hek.com Leymooo_s => I need get 1.12
Spigot 1.8.8 => I need get 1.8
Spigot 1.8.9 => I need get 1.8.9
BungeeCord 1.10-1.12x => I need get 1.12
Spigot 1.5.2 => I need 1.5.2
1.8.8 (Spigot) => I need get 1.8
1.9.2 Vanilla => 1.9
CraftBukkit 1.7.2 => I need get 1.7.2
BungeByGame 1.x-1.12.2 => I need get 1.12.2
Requires MC 1.8/1.9/1.10/1.11/1.12 => I need get 1.12
1.8.8 (CraftBukkit on Bukkit 1.8.8-R0.1-SNAPSHOT) => 1.8.8

I have regex:

$version = '1.8.8 (Spigot)';

preg_match('~^.*[ -]\K\d+(?:\.\d+)*~m', $version, $result);

But not working with string in variable $version.. And maybe with other strings..

6
  • Ger rid of * after (?:\.\d+), that makes it match 1.8.8 instead of just 1.8. Commented May 5, 2018 at 6:25
  • Read example below it. @Barmar Commented May 5, 2018 at 6:27
  • [ -] means that the version number has to be after space or -. But 1.8.8 is at the beginning of the string, there is no space before it. Commented May 5, 2018 at 6:30
  • Perhaps you'd like to hire revo to complete this project for you. stackoverflow.com/q/50079143/2943403 You are leaning on StackOverflow pretty hard (~9 questions in about 24 hours is pretty extreme). Are you trying to self-solve your questions or are you rushing straight to StackOverflow when you can't instantly figure it out. We are here to help you, but we expect you to actually toil and research before posting. Commented May 6, 2018 at 21:37
  • Please do not mix your actual input strings with what you are trying to extract. Providing two separate code blocks makes your question easier to read. Commented May 6, 2018 at 21:40

1 Answer 1

1

Update

Use a negative lookbehind to make sure that there is no version character or English letter left after a digit:

^.*\K\d(?<![a-z\d.].)\d*(?:\.\d+)*+

Live demo

PHP code (see demo):

$version = '1.8.8 (CraftBukkit on Bukkit 1.8.8-R0.1-SNAPSHOT)';
preg_match('~^.*\K\d(?<![a-z\d.].)\d*(?:\.\d+)*+~mi', $version, $result);
print_r($result[0]); // 1.8.8
Sign up to request clarification or add additional context in comments.

5 Comments

String: 1.8.8 (CraftBukkit on Bukkit 1.8.8-R0.1-SNAPSHOT), Version: 0.1 Why 0.1? With String: Requires MC 1.8/1.9/1.10/1.11/1.12, Version: 1.8 works.
Because it wasn't in your sample inputs?! Regular Expressions need a solid structure to be known beforehand to find a pattern. You shouldn't expect a regex to work magically on input which you think they should work and you didn't consider. Please update your question with all possible input formats.
Ok. Thanks very much!
@revo this is Unclear, right? Spigot 1.8.8 => I need get 1.8 versus Spigot 1.8.9 => I need get 1.8.9 What is the logic? .8 is bad and .9 is good?
@mickmackusa Yes, I noticed it and pointed to it in my first comment on OP. I assumed OP made a simple mistake otherwise there couldn't be a way to separate matches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.