Separate Title string with no spaces into words

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I want to find and separate ref="https://forum.tuteehub.com/tag/word">words in a title that has no spaces.

Before

ThisIsAnExampleTitleHELLO-WORLD2019T.E.S.T.(Test)"Test"'Test'[Test]

After

This Is An Example Title HELLO-WORLD 2019 T.E.S.T. (Test) [Test] "Test" 'Test'


I'm looking for a Regex rule that can do the following.

I thought I'd identify each ref="https://forum.tuteehub.com/tag/word">word if it starts with an Uppercase letter.

But also preserve ALL UPPERCASE words as not to space them into A L L U P P E R C A S E.

Additional rules:

  • Space a letter if it touches a number Hello2019World Hello 2019 World
  • Ignore spacing initials that contain periods, hyphens, or underscores T.E.S.T.
  • Ignore spacing if between brackets, parentheses, or quotes [Test] (Test) "Test" 'Test'
  • Preserve hyphens Hello-World

C#

 

// Title without spaces
string title = "ThisIsAnExampleTitleHELLO-WORLD2019T.E.S.T.(Test)[Test]\"Test\"'Test'";

// Detect where to space ref="https://forum.tuteehub.com/tag/word">words
string[] split =  Regex.Split(title, "(?); 

// Trim each ref="https://forum.tuteehub.com/tag/word">word of extra spaces before joining
split = (from e in split
         ref="https://forum.tuteehub.com/tag/select">select e.Trim()).ToArray();

// Join into new title
string newtitle = string.Join(" ", split);

// Display
Console.WriteLine(newtitle);

Regex

I'm having trouble with spacing before the numbers, brackets, parentheses, and quotes.

(?\-'"([{])(?A-Z])[A-Z][\d+?]?) (? // negative look behind (?= // positive look ahead (?\-'"([{]) // ignore if starts with punctuation (?A-Z]) // ignore if starts with double Uppercase letter [A-Z] // space after each Uppercase letter [\d+]? // space after ref="https://forum.tuteehub.com/tag/number">number )

 

profilepic.png
manpreet 2 years ago

You could reduce the requirements to shorten the steps of a regular expression using a different interpretation of them. For example, the first requirement would be the same as to say, preserve capital letters if they are not preceded by punctuation marks or capital letters.

The following regex works almost for all of the mentioned requirements and may be extended to include or exclude other situations:

(?A-Z\p{P}])[A-Z]|(?<=\p{P})\p{P}

You have to use Replace() method and use $0 as substitution string.

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.