简体   繁体   中英

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 "_". This code is returning:

Multidimensional E_pressions _MDX_ Developer

matching only the x. My expected output is:

ux_ui

So how would I get this positive lookbehind to match "ux" and not just x?

There are other slashes in this column and so I'd like to match only the slash found with the phrase "ux/ui".

Note that [x(?=/)] matches any single char that is either x , ( , ? , = , / or ) . It is equal to [x()=?/] .

If you meant to match any x that is followed with / , you need to remove the square brackets, x(?=/) . However, your question sounds as replace the only the "/" and no other character, preceding "ui" or directly following "ux", with an "_". Then, you can use

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

Details

  • (?<=ux)/ - / that is immediately preceded with ux - | - or
  • /(?=ui) - / that is immediately followed with ui .

See the regex demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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