Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


RegEx replace char period char


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Headkaze   Members   -  Reputation: 585

Like
0Likes
Like

Posted 12 September 2012 - 08:38 PM

I need to process some text using RegEx to change any occurance of a character, period then character to a character, period, space then character. It also needs to ignore a period with a newline, period or space after it.

Eg. This is a test.This is a test => This is a test. This is a test

Also I need to have an option to have the space be a newline character instead.

I'm not sure if I can use RegEx.Replace because I need to include the characters around the period in the result.

Edited by Headkaze, 12 September 2012 - 08:40 PM.


Sponsor:

#2 Headkaze   Members   -  Reputation: 585

Like
0Likes
Like

Posted 13 September 2012 - 02:53 AM

This is what I have so far but it doesn't seem to be ignoring the characters correctly

public static string FixCharDotChar(string str, bool addNewLine)
{
Regex regex = new Regex(@".\.[^ \n\.]");
foreach (Match match in regex.Matches(str))
{
  if (match.Length != 3)
   continue;
  string strMatch = match.Value;
  strMatch = strMatch.Insert(2, (addNewLine ? Environment.NewLine : " "));
  str = str.Replace(match.Value, strMatch);
}
return str;
}

Edited by Headkaze, 13 September 2012 - 03:06 AM.


#3 ShadowValence   Members   -  Reputation: 380

Like
0Likes
Like

Posted 13 September 2012 - 06:02 PM

You may want to try [source lang="csharp"]\r\n[/source] as your newline. Memory is a bit hazy, but it may help.

Alternatively you could use:
[source lang="csharp"] Environment.NewLine [/source] as you are in your string insertion logic.

http://www.dotnetperls.com/newline

Edited by ShadowValence, 13 September 2012 - 06:05 PM.


#4 Cornstalks   Crossbones+   -  Reputation: 6995

Like
1Likes
Like

Posted 13 September 2012 - 06:18 PM

Works for me
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
	  string str = "Hello.world. This..is a test.\nso yeah...";
	  Console.WriteLine(new Regex(@"(.)\.([^\s\.])").Replace(str, "${1}. ${2}"));							
   }
}

(results)

[edit]

To explain the magic, they're called capture groups. This page does a decent-ish job explaining capture groups (though he focuses on named capture groups (I used unnamed capture groups, hence why I referred to them by their number)).

Edited by Cornstalks, 13 September 2012 - 06:24 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#5 Headkaze   Members   -  Reputation: 585

Like
0Likes
Like

Posted 13 September 2012 - 11:59 PM

Thanks guys I appreciate your feedback.

#6 NightCreature83   Crossbones+   -  Reputation: 3287

Like
0Likes
Like

Posted 14 September 2012 - 03:39 AM

Using Environment.Newline is still adviced though in C# as new lines could be set to \n\r or \r or \n, the environment one will input the correct one in each circumstance.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS