简体   繁体   English

在字母序列(ux)之前获得正斜杠的正则表达式

[英]regular expression to obtain forward slash before sequence of letters (ux)

I'm using the following code:我正在使用以下代码:

df["profile"].str.replace('[x(?=/)]', '_')

to try to replace the only the "/" and no other character , preceding "ui" or directly following "ux", with an "_".尝试用“_”替换唯一的“/”而不是其他字符,在“ui”之前或直接在“ux”之后。 This code is returning:此代码返回:

Multidimensional E_pressions _MDX_ Developer

matching only the x.仅匹配 x。 My expected output is:我的预期输出是:

ux_ui

So how would I get this positive lookbehind to match "ux" and not just x?那么我如何让这种积极的后视匹配“ux”而不仅仅是 x?

There are other slashes in this column and so I'd like to match only the slash found with the phrase "ux/ui".此列中还有其他斜杠,因此我只想匹配与短语“ux/ui”一起找到的斜杠。

Note that [x(?=/)] matches any single char that is either x , ( , ? , = , / or ) .请注意, [x(?=/)]匹配任何单个字符,即x(?=/) It is equal to [x()=?/] .它等于[x()=?/]

If you meant to match any x that is followed with / , you need to remove the square brackets, x(?=/) .如果您打算匹配任何后面跟有/ x ,则需要删除方括号x(?=/) However, your question sounds as replace the only the "/" and no other character, preceding "ui" or directly following "ux", with an "_".但是,您的问题听起来像是只用“/”代替“ui”之前或“ux”之后直接用“_”代替其他字符。 Then, you can use然后,您可以使用

 df["profile"] = df["profile"].str.replace('(?<=ux)/|/(?=ui)', '_')

Details细节

  • (?<=ux)/ - / that is immediately preceded with ux - | (?<=ux)/ - /紧跟在ux之前 - | - or - 或者
  • /(?=ui) - / that is immediately followed with ui . /(?=ui) - /紧跟在ui

See the regex demo .请参阅正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM