Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression .net flavor

Dont ask how this works but currently it does ("^\|*(.*?)\|*$") ... kinda. This removes all extra pipes, part one, I have searched all over no anwser yet. I am using VB2011 beta, asp web form, vb coding though!

I want to capture the special character pipe (|) which is used to separate words i.e. car|truck|van|cycle.

The problem is that users often lead with, trail with, use multiple, and use spaces before and after each pipe i.e. |||car||truck | van || cycle.

Another example: george bush|micheal jordon|bill gates|steve jobs <-- this would be correct but when I do remove space it takes the correct spaces out.

So I want to get rid of any white space leading, trailing, any space before | and space after | and only allow one pipe (|) in between alphanumeric characters of course.

like image 262
user1440109 Avatar asked Jul 12 '26 18:07

user1440109


1 Answers

Requirements:

  • Remove any leading or trailing pipes
  • "Trim" whitespace around inner terms
  • Remove "multiple pipes at one time"

And these are some sample inputs -> outputs:

"|||car | boat|||" -> "car|boat"
"george bush|micheal jordon|bill gates|steve jobs"
        -> "george bush|micheal jordon|bill gates|steve jobs"
"     george bush|micheal jordon  |bill gates |steve jobs      " 
        -> "george bush|micheal jordon|bill gates|steve jobs"
"123|||123" -> "123|123"

And your example that almost works for you:

("^\|*(.*?)\|*$")

Before we go any further, it's a good idea to mention this MSDN reference page: http://msdn.microsoft.com/en-us/library/az24scfc.aspx

And this online test page: http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

My regex-fu is not strong enough, because I thought that this regex might work, but it looks like a tough job. I documented inline, but it's still complex (and it completely doesn't work)

^(?:\|*)((?:\s*)([a-zA-Z0-9]?[a-zA-Z0-9 ]*[a-zA-Z0-9]?)(?:\s*)\|?(?:\|*))(?:\|*)$

^                                     - start the line/input
(?:\|*)                               - capture any pipes at the beginning but ignore them
(                                     - begin matching so we can get the values out the other side
(?:\s*)                               - trim leading spaces
[a-zA-Z0-9]?[a-zA-Z0-9 ]*[a-zA-Z0-9]? - match any alphanumerics with spaces in between
(?:\s*)                               - trim trailing spaces
\|                                    - match any one pipe
(?:\|*)                               - ignore any remaining pipes in a row
)*                                    - end matching, we should be done
(?:\|*)                               - capture any pipes at the end but ignore them
$                                     - end of the line/input

So, let's try and solve the problem, shall we?

You should split on pipes, look ahead and see if the next one is a empty length string and if not add it to the existing length of words. Let's try that:

(I'm going to use DotNetPad for this part) http://dotnetpad.net/ViewPaste/4bpRXD-vZEOwqTLDQbEECg

Here's a sample app that does what you need, with a minimum of fuss:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class DotNetPad {
    public static void Main(string[] args) {
        string[] tests = new[] {
            "|||car | boat|||",
            "george bush|micheal jordon|bill gates|steve jobs",
            "     george bush|micheal jordon  |bill gates |steve jobs      ",
            "123|||123"
        };

        foreach(var s in tests)
        Console.WriteLine(CleanString(s));
    }
    public static string CleanString(string input) {
        string result = string.Empty;

        string[] split = input.Split(new[] {
            '|'
        });

        foreach(var s in split) {
            if (!string.IsNullOrEmpty(s)) {
                result += "|" + s.Trim();
            }
        }
        return result.Substring(1);
    }
}

I spent at most 10 minutes on the second code, and everything since I edited the post trying to get the regex to work. The moral of the story: only do the work you have to, you don't have to use a regex for everything.

like image 79
jcolebrand Avatar answered Jul 14 '26 08:07

jcolebrand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!