ability to strip all leading whitespaces from text #823
-
I have loaded the lines of text from file and I would like to know what is the best way to use regex to replace all starting whitespaces I tried the code that allowed me to strip all trailing whitespaces from text |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 8 replies
-
Your regex matches neither leading nor trailing whitespace though. It matches things like If my answer is unhelpful, then the next step here would be to share a small Rust program that I'm able to compile and run, the input to that program, the actual output you're seeing and your desired output. |
Beta Was this translation helpful? Give feedback.
-
the point is that I have an indented rust code with spaces and I would
like to replace them with nothing so that my lines of code start with
the code but no longer with spaces
2021-12-09 18:41 GMT+01:00, Andrew Gallant ***@***.***>:
… Your regex matches neither leading nor trailing whitespace though. It
matches things like `< >`. If you want to match leading whitespace, then
use `^\s*`. To match trailing whitespace, use `\s*$`. You're also not
"stripping" whitespace. You're replacing things like `< >` with `><`.
If my answer is unhelpful, then the next step here would be to share a small
Rust program that I'm able to compile and run, the input to that program,
the actual output you're seeing and your desired output.
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (comment)
|
Beta Was this translation helpful? Give feedback.
-
the tip you suggested removed the starting whitespaces from the first
line but the other lines were left with spaces at the beginning
2021-12-09 18:51 GMT+01:00, Andrew Gallant ***@***.***>:
… Apologies, but I don't understand what the problem is. Which is why I said
this:
> then the next step here would be to share a small Rust program that I'm
> able to compile and run, the input to that program, the actual output
> you're seeing and your desired output
Let's start there please.
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (reply in thread)
|
Beta Was this translation helpful? Give feedback.
-
here is the code from playground which can remove whitespace from the
first line of text but omits the other lines
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7d14d90f881c1ffc9b390f78c917e1a3
2021-12-09 19:04 GMT+01:00, Andrew Gallant ***@***.***>:
… Please share a small Rust program that I'm able to compile and run, the
input to that program, the actual output you're seeing and your desired
output.
I can't keep asking the same question in different ways. This is my last
attempt.
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (reply in thread)
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
thanks your tip solved my problem but i have a question if this
indentation is good in terms of performance? or could there be
something better to improve?
here is final code that including your tip with regex:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b075a44a6ecb66a535b543bd1c64f367
2021-12-09 19:45 GMT+01:00, Andrew Gallant ***@***.***>:
… `^` only matches at the beginning of text. You need to enable multi-line
mode so that it matches both at the beginning of text and at the start of a
line. So, `(?m)^\s*` should do the trick:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=36b37294850c99adc515d03c2ac6fa23
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (comment)
|
Beta Was this translation helpful? Give feedback.
-
I think it's fast enough but regex causes me that if I had 1 line with
code 1 empty line and another 1 line with code then empty line will be
deleted and I would like to solve it if possible
2021-12-09 20:49 GMT+01:00, Andrew Gallant ***@***.***>:
… Seems fine to me, but it's just a guess. If you're concerned about
performance, you should come up with multiple strategies and benchmark each
of them. Once you have benchmarks, you can profile the code to find the hot
spots.
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (reply in thread)
|
Beta Was this translation helpful? Give feedback.
-
this code that is on the playground works in such a way that it
removes spaces but also does that in removing empty lines and it would
be better if he left those empty lines there because they are useful,
for example, when I'm unindenting main.rs where there will be free
lines to separate functions
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=361e8c1b2abaae4189bb85717464b1e4
2021-12-09 21:01 GMT+01:00, Andrew Gallant ***@***.***>:
… Please share a small Rust program that I'm able to compile and run, the
input to that program, the actual output you're seeing and your desired
output.
I am personally finding it very difficult to help you. Your comments are
short and difficult for me to comprehend. I cannot keep going back-and-forth
like this. You might have better luck with something more structured like
StackOverflow.
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (reply in thread)
|
Beta Was this translation helpful? Give feedback.
-
Sure. Just don't match newline characters then: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0f1b3a129aaaea738692137d5ae390e8 |
Beta Was this translation helpful? Give feedback.
-
you just solved all the problems I had in this code thank you and in
the end I just want to ask where can I learn regex sintax better? and
what will regex do if in the text will be non utf 8 character?
2021-12-10 18:48 GMT+01:00, Andrew Gallant ***@***.***>:
… Sure. Just don't match newline characters then:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0f1b3a129aaaea738692137d5ae390e8
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (comment)
|
Beta Was this translation helpful? Give feedback.
-
could you tell me that how could the following be done? if regex
finds somewhere {then it puts it on a new line with the fact that
--\r\n as we talked before and with the fact that if it finds the
combination {} together then it will ignore it for example code like
use crate::{Test1, Test2}; should look like
use crate::
{
test1, test2
};
the same can be applied for playground link i sent before
2021-12-10 19:51 GMT+01:00, Andrew Gallant ***@***.***>:
… You're using strings, which are guaranteed to be valid UTF-8. If you need to
deal with non-UTF-8, you can use the `bytes` sub-module. But everything
should just work.
As for learning regex, I'd recommend just checking out the syntax docs for
this crate: https://docs.rs/regex/latest/regex/#syntax
There's also this for more general stuff:
https://www.regular-expressions.info/
Otherwise, maybe search around for regex tutorials.
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (reply in thread)
|
Beta Was this translation helpful? Give feedback.
-
i didn't post code because i have no idea how to do this but i would
like if code that will be at the end of this message can do:
if regexfinds somewhere {then it puts it on a new line with the fact that
--\r\n as we talked before and with the fact that if it finds the
combination {} together then it will ignore it for example code like
use crate::{Test1, Test2}; should look like use crate::
{
test1, test2
};
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2d323d8c10db4df1ce18898a3f6ffb56
2021-12-14 19:31 GMT+01:00, Andrew Gallant ***@***.***>:
… As with before: please share a small Rust program that I'm able to compile
and run, the input to that program, the actual output you're seeing and your
desired output.
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (reply in thread)
|
Beta Was this translation helpful? Give feedback.
-
here is for example code like:
use dotenv::var;
pub fn get_env(val: &str) -> String {
match var(val) {
Ok(v) => v,
_ => String::from(""),
}
}
and i would like if regex can reformat this code to be like:
use dotenv::var;
pub fn get_env(val: &str) -> String
{
match var(val)
{
Ok(v) => v,
_ => String::from(""),
}
}
or code like:
use std::ffi::{CStr,CString};
should look like:
use std::ffi::
{
CStr,CString
};
2021-12-14 19:53 GMT+01:00, Andrew Gallant ***@***.***>:
… I'm asking for code because I can't understand your English. If you can't
give me code, give me inputs and outputs.
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (reply in thread)
|
Beta Was this translation helpful? Give feedback.
-
I think my problem can be solved when there is a closed issue in which
someone requested to be able to create multiple matches and was told
to wait for the automata
2021-12-14 20:24 GMT+01:00, Andrew Gallant ***@***.***>:
… I don't know, sorry. I don't have the time to build regexes for you like
this. It kind of looks like regex is the wrong tool for this job. You might
do better with a Rust parser and a pretty printer. Maybe look at rustfmt.
Otherwise, for general regex help, I'd recommend other forums.
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#823 (reply in thread)
|
Beta Was this translation helpful? Give feedback.
Sure. Just don't match newline characters then: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0f1b3a129aaaea738692137d5ae390e8