Home > Back-end >  Fetch value between 2 characters from a string
Fetch value between 2 characters from a string

Time:05-21

I am getting below string as output

RX Power:    -3.4    dBm (460.9uW)

I tried below code but I am getting output like

:    -3.4    d

I want output

-3.4
$pattern = '(?s)\:.*? d '
[regex]::Matches($RXTX_Data[0], $pattern).Value

Please let me know what is missing here

CodePudding user response:

Try \s(-?\d .?\d )\s for your pattern.


  • \s for whitespace character on either side of number
  • -? for checking if '-' is in number group
  • .? for checking if '.' is in number group
  • \d for matching 1 or more numbers in number group
  • () for specifying the group you wish to capture i.e. the number group

It worked for me on https://regex101.com. If you haven't heard of it, it's a good place to practice regex.

CodePudding user response:

(Assumed you are looking to get the dBm value)

You can just select the text between the keywords using.

RX Power:(.*)dBm

This will give you an answer that includes the spaces (leading and trailing) - but you can easily remove those.

$pattern = 'RX Power:(.*)dBm'
[regex]::Matches($RXTX_Data[0], $pattern).Value.Replace(" ","")

I'm sure you could remove it in regex as well but I could only get the leading spaces removed using:

RX Power:\s*(.*)dBm
  • Related