0

I want to extract the number sandwiched between two specific letters.

e.g. string: x23y4z90

I specify x and y , I get 23
I specify y and z , I get 4
I specify z and x , I get 90 (the string pattern loops)

x\dy yields x23y, but I don't want the letters included.

*note: This is to read sensor values serially in LabVIEW.

CharlesB
  • 86,532
  • 28
  • 194
  • 218

2 Answers2

0

One possibility is to use groups:

x(\d+)y

Now, the second group will contain only the number. The first group will be the whole match.

Another possibility is to use positive lookahead and positive lookbehind:

(?<=x)\d+(?=y)

Please note the + I added. This is necessary to match numbers with multiple digits.

Check it here for x and y and here for y and z.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
0

You need to use lookarounds or groups

(?<=x)\d+(?=y)
-----    ----
 |         |->only checks if y is after a digit(lookahead) 
 |->only checks if x is before a digit(lookbehind)
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • Perfect. I can only accept one answer, and Daniel had the nice links. But thanks so much for the quick and correct reply. – user2086169 Feb 23 '13 at 04:25