Sumit Gupta
Sumit Gupta

Reputation: 2192

Regular Expression Pattern for C# with matches

I am working on project where I need to find Frequency from a given text. I wrote a Regular expression that try to detect frequency, however I am stuck with how C# handle it and how exactly I use it in my software

My regular experssion is (\d*)(([,\.]?\s*((k|m)?hz)*)|(\s*((k|m)?hz)*))$

And I am trying to find value from

Further Explanation:

  1. System needs to work for US and Belgium Culture hence, 23.2 (US) = 23,2 (Be)
  2. I try to find a Digit, followed by either khz,mhz,hz or space or , or .
  3. If it is , or . then it should have another Digit followed by khz, mhz, hz

Any help is appericated.

Upvotes: 0

Views: 253

Answers (1)

Eugene Ryabtsev
Eugene Ryabtsev

Reputation: 2301

Running replace

(\d+(?:[.,]\d+)?)\s*([KkMm]?[Hh][Zz])

for ($1 $2) gives

(23,2 Hz)
(24,4 Hz)
(25,0 Hz)sadf
(26 Hz)
(27 Khz)
(28 hz)zhzhzhdhdwe
29
(30.4 Hz)
(31.8 Hz)
4343.(34.234 Khz)
65SD

Any corrections to desired behavior?

upd: this is with negative lookbehind and word boundary:

(?<![.,0-9])(\d+(?:[.,]\d+)?)\s*([KkMm]?[Hh][Zz])\b

(23,2 Hz)
(24,4 Hz)
25,0 Hzsadf
(26 Hz)
(27 Khz)
28hzzhzhzhdhdwe
29
(30.4 Hz)
(31.8 Hz)
4343.34.234 Khz
65SD

Upvotes: 1

Related Questions