Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regex

Regex

Scheduled Pinned Locked Moved C#
regex
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    surfman19
    wrote on last edited by
    #1

    int counter = 0; ArrayList Numbers = new ArrayList(); Regex regex = new Regex(@"(.+);(.+);(.+)"); // only for 3 like "Gerald; 0650123456; Mike" ;-(((( Match matchit = regex.Match("Gerald; 0650123456; Sabine; Spech; 06761233333"); for (int i = 0; i < matchit.Groups.Count; i++) { Numbers.Add(matchit.Groups[i].Value); MessageBox.Show(Numbers[i].ToString().Trim()); } hi the regex here don't work...i want to make it, that it works for: "Gerald; 0650123456; Sabine; Spech; 06761233333" result: Numbers[0] = Gerald; Numbers[1] = 0650123456; Numbers[2] = Sabine; Numbers[3] = Spech; Numbers[4] = 06761233333; and for: "Gerald; 0650123456; Mike" result: Numbers[0] = Gerald; Numbers[1] = 0650123456; Numbers[2] = Mike; the regex have to be independent from the length of the input string.... cu

    U 1 Reply Last reply
    0
    • S surfman19

      int counter = 0; ArrayList Numbers = new ArrayList(); Regex regex = new Regex(@"(.+);(.+);(.+)"); // only for 3 like "Gerald; 0650123456; Mike" ;-(((( Match matchit = regex.Match("Gerald; 0650123456; Sabine; Spech; 06761233333"); for (int i = 0; i < matchit.Groups.Count; i++) { Numbers.Add(matchit.Groups[i].Value); MessageBox.Show(Numbers[i].ToString().Trim()); } hi the regex here don't work...i want to make it, that it works for: "Gerald; 0650123456; Sabine; Spech; 06761233333" result: Numbers[0] = Gerald; Numbers[1] = 0650123456; Numbers[2] = Sabine; Numbers[3] = Spech; Numbers[4] = 06761233333; and for: "Gerald; 0650123456; Mike" result: Numbers[0] = Gerald; Numbers[1] = 0650123456; Numbers[2] = Mike; the regex have to be independent from the length of the input string.... cu

      U Offline
      U Offline
      Uri Lavi
      wrote on last edited by
      #2

      Try to use the following regex expression: Regex regex = new Regex(@"[\w\d ]+;[\w\d ]+;[\w\d ]+"); the expression stands for:

      at least one word, digit or space followed by semicolon
      and
      at least one word, digit or space followed by semicolon
      and
      at least one word, digit or space.

      Let me know if it's ok Uri

      S 1 Reply Last reply
      0
      • U Uri Lavi

        Try to use the following regex expression: Regex regex = new Regex(@"[\w\d ]+;[\w\d ]+;[\w\d ]+"); the expression stands for:

        at least one word, digit or space followed by semicolon
        and
        at least one word, digit or space followed by semicolon
        and
        at least one word, digit or space.

        Let me know if it's ok Uri

        S Offline
        S Offline
        surfman19
        wrote on last edited by
        #3

        hi, it dont works;-( cu

        U 1 Reply Last reply
        0
        • S surfman19

          hi, it dont works;-( cu

          U Offline
          U Offline
          Uri Lavi
          wrote on last edited by
          #4

          Hi, Maybe I didn't understand the requirements of your regex, because the regex I have provided returns a valid value for exectly 3 words or digit sets seperated by a comma. Could you provide me with more info about the results you are expecting to have ?

          S 1 Reply Last reply
          0
          • U Uri Lavi

            Hi, Maybe I didn't understand the requirements of your regex, because the regex I have provided returns a valid value for exectly 3 words or digit sets seperated by a comma. Could you provide me with more info about the results you are expecting to have ?

            S Offline
            S Offline
            surfman19
            wrote on last edited by
            #5

            the regex should not only works for 3 words! for 9 words too... looks like that: "Gerald; 067612345; Sabine" or "Gerald; 067612345; Sabine; Mark; 065012233" than i want to have the names or numbers in an array: array[0] = Gerald array[1] = 067612345 array[2] = Sabine array[3] = Mark array[4] = 065012233 cu

            U 1 Reply Last reply
            0
            • S surfman19

              the regex should not only works for 3 words! for 9 words too... looks like that: "Gerald; 067612345; Sabine" or "Gerald; 067612345; Sabine; Mark; 065012233" than i want to have the names or numbers in an array: array[0] = Gerald array[1] = 067612345 array[2] = Sabine array[3] = Mark array[4] = 065012233 cu

              U Offline
              U Offline
              Uri Lavi
              wrote on last edited by
              #6

              Hi, * use the following expr. @"(?>\w|\d)+;?)" * use the Matches method and not the Match method as follows: ArrayList Numbers = new ArrayList(); Regex regex = new Regex(@"(?>\w|\d)+;?"); // only for 3 like "Gerald; 0650123456; Mike" ;-(((( MatchCollection collection = regex.Matches("Gerald; 0650123456; Sabine; Spech; 06761233333"); foreach(Match m in collection) { Console.WriteLine("**" + m.Value); } -- modified at 12:12 Saturday 27th August, 2005

              S U 2 Replies Last reply
              0
              • U Uri Lavi

                Hi, * use the following expr. @"(?>\w|\d)+;?)" * use the Matches method and not the Match method as follows: ArrayList Numbers = new ArrayList(); Regex regex = new Regex(@"(?>\w|\d)+;?"); // only for 3 like "Gerald; 0650123456; Mike" ;-(((( MatchCollection collection = regex.Matches("Gerald; 0650123456; Sabine; Spech; 06761233333"); foreach(Match m in collection) { Console.WriteLine("**" + m.Value); } -- modified at 12:12 Saturday 27th August, 2005

                S Offline
                S Offline
                surfman19
                wrote on last edited by
                #7

                hi, looks good! but the output is: Gerald; 0650123456; Sabine; Spech; 06761233333 i dont need the ; i can use substr but can i solve that with regex only? cu

                1 Reply Last reply
                0
                • U Uri Lavi

                  Hi, * use the following expr. @"(?>\w|\d)+;?)" * use the Matches method and not the Match method as follows: ArrayList Numbers = new ArrayList(); Regex regex = new Regex(@"(?>\w|\d)+;?"); // only for 3 like "Gerald; 0650123456; Mike" ;-(((( MatchCollection collection = regex.Matches("Gerald; 0650123456; Sabine; Spech; 06761233333"); foreach(Match m in collection) { Console.WriteLine("**" + m.Value); } -- modified at 12:12 Saturday 27th August, 2005

                  U Offline
                  U Offline
                  Uri Lavi
                  wrote on last edited by
                  #8

                  Yes :) Just remove the ;? from the regex expression i.e. use the following expression: "(?>\w|\d)+". Hope it helps...

                  S 1 Reply Last reply
                  0
                  • U Uri Lavi

                    Yes :) Just remove the ;? from the regex expression i.e. use the following expression: "(?>\w|\d)+". Hope it helps...

                    S Offline
                    S Offline
                    surfman19
                    wrote on last edited by
                    #9

                    hi, perfect, thats what i want;-) where can i learn regex? i am not that profi;-) here the code now: int counter = 0; ArrayList Numbers = new ArrayList(); MatchCollection matchCollectionNamen; Regex regexNamen = new Regex(@"(?>\w|\d)+"); matchCollectionNamen = regexNamen.Matches("Gerald; 0676123456; 06503333543; Sabine"); foreach (Match m in matchCollectionNamen) { Numbers.Add(m.Value); MessageBox.Show(Numbers[counter].ToString()); counter++; } do you have an idea how i can test for the first 4 digits when a number is recognized? only 0676, 0650, 0699, 0664 should be allowed for the first 4 digits... a second regex expression is needed? cu

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups