1

can you help me out to fetch some value from this string which is below

ldn2spc2.ebrd.comDiskUtilization-D(Disk) --> Disk Utilization 100 > 95
%(threshold)

I want to fetch the drive letter which before "Disk" in this case which is 'D'.

I have tried this regex

ebrd.comDiskUtilization-(\D*)\ --

which give D(Disk) but my requirement is to fetch only drive letter. Please help.

Jon
  • 3,573
  • 2
  • 17
  • 24
  • In which environment do you run your RegExp? Also see non-capturing groups https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-in-regular-expressions – avdg Oct 15 '19 at 10:05

3 Answers3

0

The right answer depends much by the RegEx engine you are using. Unfortunately sometimes it is not easy to know it.

I tested your text with the engine of PCRE 8.41 2017-07-05 and I get the 'D' with the regex:

((\w+[\.-])+)\K\w(?=\(Disk\))

But it does not work for example with Chrome's Javascript. For this instead I can use this:

(?<=(\w+[\.-])+)\w(?=\(Disk\))

Try them both and see which one works for you.

Sterconium
  • 559
  • 4
  • 20
0

It appears you are using Javascript. If my understanding is correct, this regex will only capture the drive letter as capture group 1:

ebrd.comDiskUtilization-([A-Za-z])\D*\ --

With that said, I tend to think Sterconium's solution is a better, more elegant solution. If you need the drive letter to be a capture group, you could put parenthesis around the \w - in which case it would be capture group 2.

0

In your pattern you are capturing in group 1 (\D*) matching 0+ times not a digit followed by a space and 2 dashes \ --.

Using \D will also match (Disk) as it does not contain a digit.

Note that you don't have to escape the space using a backslash and you do have to escape the dot to match it literally.

ebrd\.comDiskUtilization-([A-Z])\([^)]+\) --

In parts

  • ebrd\.comDiskUtilization- Match literally
  • ( Capture group 1
    • [A-Z] Match an uppercase char A-Z
  • ) Close group
  • \([^)]+\) Match from an opening till a closing parenthesis using a negated character class
  • -- Match a space and --

The drive letter will be in capturing group 1.

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70