Jump to content

C# Regular Expressions

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
Hello everyone, i've looked for C#/regex around the net, but i've yet to find something which would be appropriate for my situation ( my question will concern regular expressions more than C# itself, though i also need the C# syntax for this ).

I have a string which, after some modifications done to it, needs to match a certain pattern. Problem is, the pattern seems to me a bit more complicated than regular....regular expressions. I'll explain it in words:

The string needs to look like this:
One square bracket [
At least 1 number and at most 7
A variable ( hold on, i'll explain ! ) number of ' | ' characters
And, finally, a closing square bracket ]
The idea is, the string MUST have 10 characters, no more, no less.
2 of them are the brackets []
1-7 of them are numbers
the rest are |||| characters ( -> there will be between 1 and 7 ' | ' characters)

Obviously, my issue is related to the string max length, and the ' | ' fill-up characters.

Any help will be appreciated. Thank you.

#2
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Regex is actually my really weak spot :-)
Here is one cool link (choose Advanced Regex syntax link on page):
Regular Expressions Reference - Basic Syntax

I could ask my co-workers on monday for a solution if it's not too late? :-)

And here's another cool regex tester:
Derek Slager: A Better .NET Regular Expression Tester

#3
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
Thank you very much, will look at those links, meanwhile i will further exemplify my irregular...regular expression needs:

[123|||||]
[4556786|] - This is actually the maximum number length. Hence, there will always be a '|' terminating character.
[4|||||||] - Minimum number length.
Note, in every case, the total length of the string is 10.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Why not do the obvious regex match, and also test the length?
"[(0-9){1,7}\|*]"
I haven't tested the regex, so it may need some tweaking, but that plus a general length test should do the job.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
Thanks guys :D:D
Don't bother anymore - i found a workaround. Instead of completing the missing numbers with ||| [45||||||], i completed the section preceding the number with zero's [00000045] ( since i only needed the number between the brackets ). Padded left with zero's, and tested with this: @\[[0-9]{8}\] . Worked great :D