Y
Y
yuki2020-07-09 11:10:35
Java
yuki, 2020-07-09 11:10:35

Is it possible (if so, how?) to replace the first lowercase letter of the first word with an uppercase letter via regular expressions in java?

I have several sentences

this text has two sentences. this is the second one. Aggf. fgjdfg dfgjjdf dkkldf. FGjdfgj. lLIFgrkg

Some of them start with a lowercase letter. Is it possible to replace them with capital letters via regular expressions (java) and if so, how?
Capturing the first lowercase letters of the first words ^[az]|(?<=\.\s)[az]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-07-09
@rstJkee

It is possible and through regular, it is possible and without regular.

M
mayton2019, 2020-07-09
@mayton2019

The correct answer is no. The functionality of regular expressions is to find and indicate what is found. You can see how Pattern/Matcher works in pairs. They just find groups of expressions and point the finger, they say, this is where matching came up.
But there is a programming language API such as String::replace which allows using
regular expressions to find-and-replace in a single operation.
But this particular operation - the capitalization of the first letter in a Java string, I would do simply through substring () and toUpper (). It will be hundreds of times faster in time. we will not involve compiling the regex pattern in the process. Roughly speaking, if you decide to shoot at the birds, then take a slingshot. You don't need a gun.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question