x f
x f

Reputation: 93

How regular expressions stop matching

aws-sdk-java/1.9.4 Linux/3.10.0-862.mt20190308.130.el7.x86_64 Java_HotSpot(TM)_64-Bit_Server_VM/25.45-b02/1.8.0_45

I want to get substr 'aws-sdk-java/1.9.4'

Here is my regular

(\S+?\/\S+?)(\s|$)

but it matches many times result

is someone can help me? Thank you very much~

Upvotes: 0

Views: 64

Answers (1)

The fourth bird
The fourth bird

Reputation: 163467

You could make the pattern a bit more specific, and get a match only without capture groups.

(?<!\S)\w+(?:-\w+)*\/\d+(?:\.\w+)*(?!\S)

Explanation

  • (?<!\S) Assert a whitespace boundary to the left
  • \w+(?:-\w+)* Match 1+ word chars and optionally repeat - and 1+ word chars
  • \/ Match / (Depending on the delimiter of the pattern, you don't have to escape the /)
  • \d+(?:\.\w+)* Match 1+ digits and optionally repeat . and 1+ word characters
  • (?!\S) Assert a whitespace boundary to the right

Regex demo

Or a boader variant:

(?<!\S)[^\/\s]+\/\w+(?:\.\w+)*(?!\S)

regex demo

Upvotes: 1

Related Questions