Empty Coder
Empty Coder

Reputation: 589

Fetch value between 2 characters from a string

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

Upvotes: 0

Views: 45

Answers (2)

Almero
Almero

Reputation: 84

(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

Upvotes: 1

FlippinBits
FlippinBits

Reputation: 185

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.

Upvotes: 1

Related Questions