-3

How to get the required format text output from json payload using regexp

"number" : 4aac4c35-8e3e-4730-8364-381884d6f20f:OP:QUAL:117804471:PON:false

what is the expression to get the output like following

"number" : 117804471

could you please help.

I tried the following; but it didn't work; I am newbie to this.

([\"]token[\"][\s]*:[\s]*)?([0-9]{9})
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ram Kowsu
  • 711
  • 2
  • 10
  • 30
  • 1
    Normally, depending on your environment, you do not parse JSON yourself. There should be some kind of parser to access it via objects and properties or sth. – Chrᴉz remembers Monica Jan 24 '20 at 10:45
  • What you want is not really clear. Do you want to extract a part of the given output? Where does is come from? Add some explanation and context to help us help you. – PJProudhon Jan 24 '20 at 10:47
  • @ChrᴉzsaysreinstateMonica, Got it. but I am trying in my local system, for one usecase. could you please help – Ram Kowsu Jan 24 '20 at 10:48
  • @PJProudhon. I am validating here, https://regex101.com/r/9WgsVb/2 – Ram Kowsu Jan 24 '20 at 10:49
  • @Creator: Have you got more examples of what "number" could contain? Is the number always 9 symbols long as you expect in your regex? Is "QUAL" or "PON" always present or not? – Patrick Janser Jan 24 '20 at 11:16
  • @Pj, Yes, always it will be there – Ram Kowsu Jan 24 '20 at 11:36
  • What language/tool are you using? From the [regex tag info](https://stackoverflow.com/tags/regex/info): "Since regular expressions are not fully standardized, all questions with this tag should also include a tag specifying the applicable programming language or tool." – Toto Jan 24 '20 at 11:39
  • @Toto, this is for iRule configuration, for loadbalancing – Ram Kowsu Jan 24 '20 at 11:39
  • A regex matches only consecutive characters. What you want can't be done with a simple match, you have to match 2 distinct substrings and concatenate them progammaticaly. – Toto Jan 24 '20 at 12:31
  • iRule just takes the regularExp – Ram Kowsu Jan 24 '20 at 12:41

2 Answers2

2

Yes, as @PJProudhon says, we are lacking some precisions to help you.

If you just want to extract the number without using some capturing groups, you could go with this regular expression: (?<=:)\d+(?=:\w+:(?:true|false))

Quick explanation:

  • (?<=:) is a positive lookbehind to find the : char.
  • \d+ is to get a number of at least 1 decimal or more.
  • (?=:\w+:(?:true|false)) is a positive lookahead to find a :, a word, a : and then true or false.

Small test with multiple values here: https://regex101.com/r/1nRnct/2

Hope this can help you.

Patrick Janser
  • 3,318
  • 1
  • 16
  • 18
1

Still not very sure of what you exactly want or need, but let's give a try, based on what you wrote here and in your sample.

Receiving some string respecting the following:

  • Starting with a double-quote enclosed token.
  • Followed by a colon, occasionally surrounded by any number of spacing characters.
  • Followed on the right part by a suit of colon separated tokens.
  • One of those being composed of numbers only.

Trying to replace that input by only:

  • Double-quote enclosed token.
  • Followed by a colon, surrounded spaces kept.
  • Immediately followed by the number only token.

You could then use the following pattern to get your match: ^(?<token>"[^"]+"\s*:\s*)(?:\d*[^\d:][^:]*:)*(?<number>\d+)(?::[^:]*)*$.

The following should then be used to replace: $1$2.

Demo here.

That may be simplified or adapted when knowing additional rules.

PJProudhon
  • 835
  • 15
  • 17