1

So I have this text:

Presedintele executiv al Miscarii Populare, Eugen Tomac, sustine ca sediul partidului din strada Pajurei, Sector 1, a fost vandalizat si ca in spatele acestei actiuni s-ar afla &, scrie Mediafax. &, a scris Eugen Tomac pe Facebook.Tomac a mai sustinut, intr-o declaratie de presa, ca locuitorii din strada Pajura ar fi primit mesaje, in numele PMP. &, a mai spus presedintele executiv.&, a mai sustinut Tomac, precizand ca a depus deja sesizare la politie.

which contains the & character inside. Every sentence ends with a . .

I have this regex: [^\.]*\&[^\.]*\.? that matches any sentence that contains the & character inside of them and then removes them, but I don't want to remove that sentence if its longer than say n characters, how do I exactly specify that in the regex?

1 Answer 1

1

If we'd say n would be 40, you could build a regex like (?<=\.|^)(?![^\.]{40,})[^\.&]*\&[^\.]*\.?. This will only match sentences with 40 or less characters.

It uses a negative lookahead at the start of your match to ensure this and also a lookbehind to make sure, that we don't just match partial sentences. I have also added the & to the first negatet character class you are using, as it will speed up your regex a fair bit. Not sure about the ? behind \. - you might want to remove it.

I want to note, that the dot isn't necessarily the end of a sentence, but might be part of an abbreviation.

Sign up to request clarification or add additional context in comments.

4 Comments

indeed that works, but it also replaces part of the sentence that has more than 36 characters and contains & and I don't want to modify that type of sentence. Here is regex: regex101.com/r/mS6jI1/1
@southpaw93 You are correct, sorry - adjusted it so it won't match partial sentences
amazing, it works thanks for saving me hours and hours of trials :D, just one quick question, what effect does \.|^ have in that lookbehind?
It ensures that the last char before the match is either the dot or the start of the string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.