0

I am trying to use Regex to grab a substring of a large string. The overall string has certain text, 'cow/', then any number of characters or spaces that are not digits. The first digit hit is the start of the desired substring I want. This desired substring consists of only digits and periods, the first character or space seen that is not a digit or period indicates the end of the desired substring.

For example: 'cow/ a12.34 -123'

The desired substring is '12.34'.

So far I have this regex that partially works (I think the '| .' is not entirely correct): (?<=([A-z]|[0-9])/\s*).?(?=\s[^0-9 |.])

Thanks in advance.

Adam
  • 155
  • 1
  • 2
  • 9

1 Answers1

0

This should be easy to achieve by relying on capturing groups:

cow/[^0-9]*([0-9.]+)

The group will contain the text that you want to extract, in Java group(index), in C# with Groups[index]. Other languages provide similar features.

Don't try to solve everything inside the regular expression, but leverage the power of your runtime :)


Edit after comment on the OP:

Azure Kusto has the extract(regex, captureGroup, text [, typeLiteral]) function to extract groups from regular expression matches:

extract("cow/[^0-9]*([0-9.]+)", 1, "cow/ a12.34 -123") == "12.34";

The argument 1 tells Kusto to extract the first capturing group (the expression inside the parentheses).

knittl
  • 246,190
  • 53
  • 318
  • 364