Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a regular expression

Status
Not open for further replies.

luigiida

Programmer
Mar 26, 2003
29
0
0
IT
Hi,
I have the following problem:
I have in input a sequence of strings( I read from a file) and I need to replace the "dot" with a blank space or nothing depending from the following situations:

The replace has to be with nothing when the string is of this kind: a.a.a.a
and with blank space in this situation: aa.aa.aaa.

With single characters: nothing
With multiple characters: blank space

I'm in this situation: I should parse company names and sometimes the dot is used to concatenate parts of the name, company.of.new.york, and other times to abbreviate, s.a.

Can you, please, help me, it's very important??

Thanks
Luigi
 
I'm reading up on how regular expressions work in C#, but since I'm a perl man at heart, I'll throw this out for the time being:
Code:
s/([^\.]+)\.(?=([^\.]*))/$1.((length $1 > 1 || length $2 > 1) ? ' ' : '')/ge;
It looks for one or more non . characters [^\.]+ then a . It looks ahead to see if there's any non . characters after it, but does not consume them. At this point, it has the first group of non . chars and the . itself for a "match" string that it is going to replace. It always prints the first character group, and it tests if either of the two character groups have a length greater than one. If that's true, then it concatentates a space on the end. If not, nothing.

At this point it goes back into the string at the place it left and trys to match and substitute again. This is what I meant about not consuming the characters in the second group.

Again, I'm looking at C# syntax to see how it would work there. I don't have a compiler in front of me right now, so it's going to be pretty heavily based on theory. I'll try and get back shortly after I find something, but in case you are already familliar with C# regex syntax, I'd throw it out there for you now. Lots of languages use PCRE (Perl-Compatible Regulare Expressions) and often times there's little to no change. This one might be a little complicated for that, though. I'll see.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
This is swiped from an MSDN example and altered to what looks like will fit here. Untested, (and nowhere near as pretty as perl :)) but might work.
Code:
using System;
using System.Text.RegularExpressions;

namespace MyNameSpace
{
   class MyClass
   {
      static void Main(string[] args)
      {
         string sInput, sRegex;

         // The string to search.
         sInput = "A.Company.Name";

         // The seach regular expression string.
         sRegex = @"([^\.]+)\.(?=([^\.]*))";

         Regex r = new Regex(sRegex);
            
         MyClass c = new MyClass();

         // Assign the replace method to the MatchEvaluator delegate.
         MatchEvaluator myEvaluator = new MatchEvaluator(c.ReplaceCC);
            
         // Write out the original string.
         Console.WriteLine(sInput);

         // Replace matched characters using the delegate method.
         sInput = r.Replace(sInput, myEvaluator);
         
         // Write out the modified string.
         Console.WriteLine(sInput);
      }
    
      public string ReplaceCC(Match m)
      {
         if(m.Groups[0].Value.Length > 1 || m.Groups[1].Value.Length > 1)
         {
            return m.Groups[0].Value + " ";
         }
         return m.Groups[0];
      }
   }
}

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Regex.Replace(aString, @&quot;(?<1>a{2,})\.&quot;, &quot;${1} &quot;))

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top