Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using regex to split and replace text in a string #26

Open
jollyrancher12 opened this issue Nov 22, 2022 · 7 comments
Open

using regex to split and replace text in a string #26

jollyrancher12 opened this issue Nov 22, 2022 · 7 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@jollyrancher12
Copy link

/// Once compiled, it can be used repeatedly to search, split or replace

how can this regex be used to split / replace text in a string? i tried using the split function but is erroring with the message "required for Regex to implement Pattern<'_>". Not sure if I am misunderstanding how to use the regex to split strings or if this is a bug?

@BurntSushi
Copy link
Owner

The bug here is the docs. I likely copied them from elsewhere. I just never got around to actually implementing the split or replace routines. You can either implement them for your specific use case, or you or someone could submit a PR adding them to the API of this crate. (And if you do that, I'd suggest copying what is in the regex crate.)

In general this crate exposes PCRE2 stuff for my own purposes in ripgrep. If folks want additional stuff I'll do my best to merge patches. But I do not have time to do it myself.

@BurntSushi BurntSushi added enhancement New feature or request help wanted Extra attention is needed labels Nov 22, 2022
@BurntSushi
Copy link
Owner

Also, I would strongly urge you to use the regex crate if you can. I realize PCRE2 has a bunch of fancy features, but if you're processing untrusted input from somewhere, it's very easily to fall victim to ReDoS.

If you do want to use the regex crate but don't know how to avoid using look-around or similar fancy features, I can try to help if you want to post a question to the regex repo: https://github.com/rust-lang/regex/discussions --- If you do, just please try to include a full minimal example of what you're trying to do: your inputs, what you've tried, the output you get and the output you want.

@jollyrancher12
Copy link
Author

ah i see, yes unfortunately look arounds are necessary for our use case and are already pretty deeply embedded into our work flow.
if i can't get around the look-arounds, would you recommend using the fancy-regex crate over this pcre2 crate? i saw that fancy-regex was built on top of your rust regex crate and implements look arounds but did not see much info about its performance. (and thank you so much for your quick reply!!)

@BurntSushi
Copy link
Owner

I don't have much direct experience with fancy-regex to be honest. It isn't as mature as PCRE2, but if it works for your use case, it's worth a try. It won't save you from ReDoS though. ReDoS is a fundamental problem in pretty much any regex where you use look-around or other "fancy" non-regular features.

@priyankat99
Copy link

priyankat99 commented Dec 1, 2022

ended up adding split / replace functions if you could take a look at the PR whenever you have the time!
just ported over the logic from re_bytes.rs from the rust/regex library and tested the functions to verify they work

@zong-zhe
Copy link

Hi @priyankat99 , I would like to know what is wrong with this PR and when can I use this PR, #27, / replace functions is blocking my work.

@priyankat99
Copy link

priyankat99 commented Jul 25, 2023

hi @zong-zhe i dont think anything is wrong with the PR, it just never got reviewed. ive been using the replace and split functions for a while and its working just fine

feel free to use my branch of pcre2 for the replace functions by adding this to your Cargo.toml

pcre2 = { git = "https://github.com/priyankat99/rust-pcre2", tag = "0.0.3"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants