-1

there are some Japanese sentences like following:

 {keyword: 部屋}いいね!
 {keyword: 公園}は綺麗です.
 私は{keyword: 部屋捜査}です。

   .......... .........

I want to replace the substring like :{keyword: 部屋},{keyword: 公園}..... with 'keyword'.

For example:

input: 私は{keyword: 部屋捜査}です  

output: 私はkeywordです

My trying code is following and but it is wrong, the result is same:

import re
s = '{keyword: 賃貸}'    
t = re.sub(r"\{keyword:[あ-んア-ン一-]+\}", 'keyword', s)
print(t) 

Thanks!

tktktk0711
  • 1,656
  • 7
  • 32
  • 59

1 Answers1

-1

Use the following:-

inputString = "私は{keyword: 部屋捜査}です"
t = re.sub(r"\{keyword:[^}]*}", 'keyword', inputString)
print(t) 
nandal
  • 2,544
  • 1
  • 18
  • 23
  • Could you explain in detain, please! for example; what's meaning of '[^}]'? – tktktk0711 Jun 01 '18 at 02:09
  • Hi @tktktk0711, `[^}]` is used to define one character, that is not `}`. For further details, use reference:- https://docs.python.org/3/howto/regex.html#more-metacharacters and – nandal Jun 01 '18 at 07:08