replace or remove the characters before @temp in containing word in complete string
替换或删除@temp之前包含完整字符串中单词的字符
例如,在输出0020_b@temp中不应出现
下面是输入字符串
这可以在python或javascript中完成。两者都可以接受
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | "{ "name":"ProcessInfo", "children": [ { "name":"0020_B|Y", "children": [ { "name":"0020_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0020_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0050_B|Y", "children": [ { "name":"0050_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0050_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0030_B|Y", "children": [ { "name":"0030_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0030_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0060_B|Y", "children": [ { "name":"0060_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0060_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0040_B|Y", "children": [ { "name":"0040_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0040_B@tempTimeOfDeviation_Percentage|Y" } ] } ] } ] }" |
输出应该像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | "{ "name":"ProcessInfo", "children": [ { "name":"0020_B|Y", "children": [ { "name":"CycleTimeAdherence|Y", "children": [ { "name":"TimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0050_B|Y", "children": [ { "name":"CycleTimeAdherence|Y", "children": [ { "name":"TimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0030_B|Y", "children": [ { "name":"CycleTimeAdherence|Y", "children": [ { "name":"TimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0060_B|Y", "children": [ { "name":"CycleTimeAdherence|Y", "children": [ { "name":"TimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0040_B|Y", "children": [ { "name":"CycleTimeAdherence|Y", "children": [ { "name":"TimeOfDeviation_Percentage|Y" } ] } ] } ] }" |
你可以用下面的方法来完成这个…
JavaScript
- 使用regex
[\w_]+@temp 执行搜索,它将搜索后面跟着temp的任何单词。 - 使用replace()函数替换该单词
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | let input = `{ "name":"ProcessInfo","children": [ { "name":"0020_B|Y", "children": [ { "name":"0020_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0020_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0050_B|Y", "children": [ { "name":"0050_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0050_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0030_B|Y", "children": [ { "name":"0030_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0030_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0060_B|Y", "children": [ { "name":"0060_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0060_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0040_B|Y", "children": [ { "name":"0040_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0040_B@tempTimeOfDeviation_Percentage|Y" } ] } ] } ] }`; let output = input.replace(/[\w_]+@temp/g, ''); console.log(output); |
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import re s ="""{ "name":"ProcessInfo","children": [ { "name":"0020_B|Y", "children": [ { "name":"0020_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0020_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0050_B|Y", "children": [ { "name":"0050_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0050_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0030_B|Y", "children": [ { "name":"0030_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0030_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0060_B|Y", "children": [ { "name":"0060_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0060_B@tempTimeOfDeviation_Percentage|Y" } ] } ] }, { "name":"0040_B|Y", "children": [ { "name":"0040_B@tempCycleTimeAdherence|Y", "children": [ { "name":"0040_B@tempTimeOfDeviation_Percentage|Y" } ] } ] } ] } """ r = r"[\w_]+@temp" o = re.sub(r, '', s) print(o) |
那么?,那有什么问题?一个简单的替换函数就足够了
网址:https://www.w3schools.com/jsref/jsref eu replace.asp
https://docs.python.org/2/library/string.html string.replace