Cornwell
Cornwell

Reputation: 3410

Regex: Unable to do negative lookahed

Here's my input text:

Intel Core i3-4170 3.7GHz

I'm trying to replace 3.7GHz with XXX

Here's my pattern:

/\s(?=[^\s]+(.*?GHz)$)/

but this matches: i3-4170 3.7GHz

if I add the global flag I get what I need in the 2nd group, but I'm not sure if that's reliable.

https://regex101.com/r/ARa1tG/1

Upvotes: 0

Views: 52

Answers (3)

shA.t
shA.t

Reputation: 16958

Another way can be - more specific - :

/[\d.]*\dGHz/g

[Regex Demo]

Upvotes: 0

linden2015
linden2015

Reputation: 887

My answer:

\b\d+(?:\.\d+)?ghz\b

\b    // word boundary
\d+   // 1 or more digits
(?:   // optional group (not captured)
\.\d+ // a dot, then 1 or more digits
)?    // end of group
ghz   // literal
\b    // word boundary 
  • Flags: g, i
  • Steps: 91

Demo

Upvotes: 0

marvel308
marvel308

Reputation: 10458

you can use the regex

\b[^\s]*GHz\b

see the regex 101 demo

Upvotes: 1

Related Questions