Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to main content

Get the Reddit app

Scan this QR code to download the app now
Or check it out in the app stores
r/rust icon
r/rust icon
Go to rust
r/rust

A place for all things related to the Rust programming language—an open-source systems language that emphasizes performance, reliability, and productivity.


Members Online

Rust Regex problem in a database query

🛠️ project

I'm trying to do a substitution in a sqlite database. I'm using DB Browser for SQLite with a regex plugin that I believe uses rust.

What I've got is two consecutive blank lines that I want to reduce to one. So my query is

update content 
set data = regex_replace_all(
'^$^$', 
data,
'\n'
);

I've also tried it looking for \n\n, ($m)\n\n, and ($m)^$^$, but nothing is found.

Is there some way to do this? Thanks much.

Share
Sort by:
Best
Open comment sort options
u/Solumin avatar

This really isn't a Rust question. You need to know what regex engine or language is being used, which doesn't necessarily depend on what it's implemented in.

^$^$ should work in multiline ("m") mode. Are you sure you're setting it correctly?

\n\n can work, if a single \n indicates a newline. Some formats and environments (e.g. Windows) use \r\n instead. As far as I know, most regex engines don't have an easy way to indicate a platform-agnostic newline.

u/paul_1149 avatar

r/n did catch the line feeds, which does make sense since it's a DB for a Windows program. The problem is that I can't find the multiline flag.

The regex plugin is https://github.com/asg017/sqlite-regex

It's built using Rust, and the regex engine is a Rust crate, so I thought this would be the forum to raise the question in. I didn't get a response from the plugin creator.

The Rust regex crate is https://crates.io/crates/regex.

And its syntax is https://docs.rs/regex-syntax/latest/regex_syntax/all.html

But I haven't been able to get to first base with it.

Once I saw that r/n found linefeeds, I converted '(\r\n){1,}' to 'qqq', then converted 'qqq' to '\r\n'. The problem was that that prints as a literal, not an RTF flag.

I got around that with '(\r\n)(\r\n){1,}' / '$1', which did retain the flag of the first capture.

But that doesn't solve my general problem finding multiline matches. Even '\par\r\n' is not finding "\par" sitting alone on a line.

The answer would be to find a legend of Rust regex flags, but I haven't found one workable for me.

u/burntsushi avatar

The only thing multiline does is change the meaning of ^ and $. It does literally nothing else. So if you aren't using ^ or $, then enabling the mulitline flag is a no-op.

But you can enable it inside the syntax. The docs even tell you how: https://docs.rs/regex/latest/regex/#grouping-and-flags

u/paul_1149 avatar

Wow, thank you. Somehow I missed that. I did however try (?m) and it didn't help. Your explanation is probably why. I still need to do more work on this.

More replies
More replies

Multiline mode is generally not the default, you must enable it. Check your regex plugin's documentation.

More replies

This has nothing to do with Rust.

Ask this in r/regex

u/paul_1149 avatar

A Rust regex crate is the heart of the regex engine. I thought this would be a logical place to ask.

More replies
u/paul_1149 avatar

It's a Windows DB, and using \r\n did advance me one step, but I'm not there yet. Thanks.

More replies
u/burntsushi avatar

^$^$ can only match one zero-width position and it is precisely equivalent to ^$. So if you're trying to find two consecutive empty lines, that is certainly not going to do it.

It's otherwise impossible to help you because you haven't provided enough details about what the input is exactly. That matters. Presumably, \n\n would be sufficient for this. Or maybe \r\n\r\n. But who knows. Maybe an "empty" line actually has some whitespace in it.

You're basically asking a bunch of strangers to make a bunch of guesses for you.

You probably need to go ask whoever made the regex plugin.

u/paul_1149 avatar

Yes, it is a DB for a Windows program. Basically I was hoping for a pointer to details on the Rust regex implementation. Please see my more detailed reply above for links and steps I've taken. thanks.

u/burntsushi avatar

I did. I wrote the Rust regex engine. So I can literally tell you everything about it that you could ever possibly want to know. But what I'm telling you is that you haven't provided enough details to help you. Where is your reproduction? How am I supposed to even begin to test your scenario? Who knows. I certainly don't.

More replies
More replies