Regular expressions

Z

Zach

On 11/25/2011 7:36 PM, Zach wrote:
On 11/25/2011 10:35 PM, Arne Vajhøj wrote:
Regex.Replace(input," [^a-zA-Z ,:;!?.]+((?= ))|([^a-zA-Z
,:;!?.]+)","")

Trying out your solution, this works as well:
Regex.Replace(input," [^a-zA-Z ,:;!?.]|([^a-zA-Z ,:;!?.]+)","")

What do you intend with (?= ) in your solution?

(?= ) requires that the comes a space after the previous.

"x 2 x" should become "x x" but "x 2x" should become "x x" not "xx".
--------------------------------------------------------------------
Regex.Replace( +((?= ))
I changed your string to +(?= ) without additional () and that worked ok.
--------------------------------------------------------------------
Without +(?= ) the code works fine as well.
All the conditions seem to be fulfilled by the remaining code.
(1.) " [^a
(2.) zA-Z ,
(3.) plus the rest of your code

Then you get (code below), which works OK.

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string input = "@%^&(^&678Cat jumped over 2 walls, not 1, and said
bingo! But where am I?";
Console.WriteLine(Regex.Replace(input, " [^a-zA-Z ,:;!?.]|([^a-zA-Z
,:;!?.]+)", ""));
Console.ReadLine();
}
}
}

If you are happy with "x 2x" becoming "xx", then just stay with that.

Arne

Arne, no I am not. Thank you for pointing this out to me,
and thank you for your expert help.

Regards,
Zach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top