如何解决JS正则表达式查找替换URL的协议,并从字符串到字符串结尾
例如,我有以下字符串:
我想使用JS variant-
和正则表达式删除URL的协议.replace()
和域,因此字符串为:
颜色
大小
类型
该怎么做?我已经用以下代码尝试过了,但是没有用:
.replace("http://variant-","").replace("https://variant-","").replace(/^.[a-zA-Z]*/,"")
解决方法
嗯,有多种方法可以做到这一点。最简单的方法之一是在此正则表达式中使用positive look behind方法:
df = df.assign(count=df.groupby(['ID','City'])['City'].transform('count')).drop_duplicates()
df['max'] = np.where((df['count'] == df.groupby('ID')['count'].transform('min')),'third_frequent_city',np.nan)
df['max'] = np.where((df['count'] == df.groupby('ID')['count'].transform('median')),'second_frequent_city',df['max'])
df['max'] = np.where((df['count'] == df.groupby('ID')['count'].transform('max')),'first_frequent_city',df['max'])
df = df.drop('count',axis=1).set_index(['ID','max']).unstack(1)
仅当您的URL中的字符后跟 City
max first_frequent_city second_frequent_city third_frequent_city
ID
1 London New York Berlin
2 Shanghai NaN NaN
符号时,此字符才会匹配。
所以最终输出将是这样的:
/(?<=\-)\w+/gm
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。