Add space after specific character at the beginning of sentence.
-
Hi there.
I’m pretty new to the regex-thing, but that’s my own lazy-ass-fault. I’ve been trying to learn it a bit now (holy crap, is it overwhelming…), but I’m somewhat stuck. (see image)
After every “-” at the beginning of sentence, add a space between “-” and capital letter. (or replace “-” with "- ")
These are the lines in the image:
[INPUT] -Is that good? -That is gooder. -Scary good, but not gooder-good. -It was the goodest!
RegEx used: (-)([A-Z]) Replace with: "\1 \2"
[OUTPUT] - Is that good? - That is gooder. - Scary good, but not gooder- good. - It was the goodest!
“gooder- good” should be “gooder-good”. No space here.
My logic was: They have in common that every sentence starts with “-” + “A-Z”. I group “-” and “A-Z” and add a space between them.
The regex (-)([A-Z]) works fine in regex101.com, see image
(don’t understand why I get a pattern error in Substitution though)Where am I going wrong with this?
Thanks for the help in advance.
-
First off, Notepad++ uses the Boost regular expression library; regex101 does not; there may be slight differences in syntax between the two (though that’s not the cause of your problem, in this case).
Your problem is that by default, regex101 uses case-sensitive matching, so
[A-Z]
only matches uppercase. However, you have not checkmarked☐ Match case
in the Notepad++ Replace dialog, so Notepad++ is ignoring case, and thus[A-Z]
is actually matching[A-Za-z]
. If you want the regex to pay attention to case, use☑ Match case
. -
Thank you for noticing that. It fixed my issue.
@PeterJones said in Add space after specific character at the beginning of sentence.:
First off, Notepad++ uses the Boost regular expression library; regex101 does not; there may be slight differences in syntax between the two.
I just noticed it. Another software I’m using, uses .NET RegEx and for grouping you need to use “$1”, instead of “\1”. Things are confusing the hell out of me now. Whish it was a bit more universal. People changing things for the sake of “because I can” it seems like, but not making it easier.
Anyways, thanks again.
-
@JajinderK said in Add space after specific character at the beginning of sentence.:
Whish it was a bit more universal.
Soooo many flavors of regex out there (at least this many):
-
@JajinderK said in Add space after specific character at the beginning of sentence.:
you need to use “$1”, instead of “\1”.
I actually prefer to use
$1
in Notepad++ (note: it’s valid there) and other places as well.
With\1
I start wondering, “do I need to double this backslash the way I’m using this?”. Using$1
there is no “escaping” concern. Hopefully you know what “escaping” means.In truth, I really prefer
${1}
because if you need to do$1
followed by a literal0
, and you try doing$10
, you don’t get what you want (you get group 10 instead of group 1). Use${1}0
and you do get what you want.
Things are confusing the hell out of me now.
Oh…well…it’s just beginning, man. :-)
-
@Alan-Kilborn said in Add space after specific character at the beginning of sentence.:
Soooo many flavors of regex out there (at least this many):
That is sooooooo not funny.
But you gave me great tips regarding the
\1
and the${1}
. Thank you.It’s not only confusing me, it’s infuriating me actually, cause I really want to know how it works, but it’s not that simple if you don’t know the basics. For example the regex I made, is too simple. “What if the next character after the dash is a . (dot)” and I try to fix it with the limited knowledge what I have… uuuuugh. I’m struggling and I try to find examples, but I will get there eventually (I hope, if don’t give up … XD)
EDIT:
(-)(\.|[A-Z])
=> Just learned that\.
matches the dot XD -
@JajinderK said in Add space after specific character at the beginning of sentence.:
EDIT: (-)(.|[A-Z]) => Just learned that \ . (without the space) matched the dot XD
Yes,
\.
matches the literal dot character.Hint for the forum: put ` backticks around regex, because that makes sure that the forum software won’t change your text. So
`(-)(\.|[A-Z])`
will be rendered as(-)(\.|[A-Z])
in the forum.(I see that continued edits, it’s getting closer to rendering like you want… good job)
The Template for Search/Replace FAQ, and the Formatting Forum Posts FAQ (linked below) explain more about how to format your posts for asking these kinds of questions.
And the URL for the User Manual’s regex section will be indispensable for someone trying to actually learn how Notepad++ regex work.
----
Useful References
-
@PeterJones said in Add space after specific character at the beginning of sentence.:
@JajinderK said in Add space after specific character at the beginning of sentence.:
The Template for Search/Replace FAQ, and the Formatting Forum Posts FAQ (linked below) explain more about how to format your posts for asking these kinds of questions.
And the URL for the User Manual’s regex section will be indispensable for someone trying to actually learn how Notepad++ regex work.
Thank you so much for the links. (I hope I won’t give up.)