regular expersion. replace all except those that matches the patte

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,

i have a regular expression question here.

string: "({WBC} * {FBC} - {ABC} * 5)"
pattern: "(.*?)(\{)(.+?)(\})(.*?)"
replace: "$3 "

expected result: "WBC FBC ABC"
returned result: "WBC FBC ABC * 5)"

where did i go wrong?

any help will be much appreciated.

thanks!
Lance
 
Lance Koh said:
hi,

i have a regular expression question here.

string: "({WBC} * {FBC} - {ABC} * 5)"
pattern: "(.*?)(\{)(.+?)(\})(.*?)"
replace: "$3 "

".*?" at the end of the pattern will always match 0 characters - that's why
it's called "lazy".
expected result: "WBC FBC ABC"
returned result: "WBC FBC ABC * 5)"

As expected: " * 5)" is never matched, and never replaced.

Try something like: ".*?\{(.+?)\}[^{]*", replace with "$1 ".

Niki
 
as suggested, i tried ".*?\{(.+?)\}[^\{]*" and it worked. many thanks!

lance

Niki Estner said:
Lance Koh said:
hi,

i have a regular expression question here.

string: "({WBC} * {FBC} - {ABC} * 5)"
pattern: "(.*?)(\{)(.+?)(\})(.*?)"
replace: "$3 "

".*?" at the end of the pattern will always match 0 characters - that's why
it's called "lazy".
expected result: "WBC FBC ABC"
returned result: "WBC FBC ABC * 5)"

As expected: " * 5)" is never matched, and never replaced.

Try something like: ".*?\{(.+?)\}[^{]*", replace with "$1 ".

Niki
 
Back
Top