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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Please help on regular expression

Please help on regular expression

Scheduled Pinned Locked Moved C#
questionregexhelp
9 Posts 4 Posters 1 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.
  • L Offline
    L Offline
    lordbearsg
    wrote on last edited by
    #1

    How do i apply regular expression to find for host name from the text below and it prints out Host name: IT786P20?? Host Name: IT786P20 OS Name: Microsoft Windows XP Professional

    L 1 Reply Last reply
    0
    • L lordbearsg

      How do i apply regular expression to find for host name from the text below and it prints out Host name: IT786P20?? Host Name: IT786P20 OS Name: Microsoft Windows XP Professional

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi The question is if you really want to use a regular expression for this. wouldn't a string operation do the job? someting like

      string blah = @"Host Name: IT786P20
      OS Name: Microsoft Windows XP Professional"
      int colonIndex = blah.IndexOf(":");
      string HostName = blah.SubString(colonIndex+1,blah.IndexOf("\r",colonIndex+1)-colonIndex+1).Trim();
      Console.WriteLine(HostName);

      btw. if you want to work with regular expressions i would recommend you to get expresso it's a free tool with a great regex-Builder and Tester. here[^] greets m@u

      L 1 Reply Last reply
      0
      • L Lost User

        Hi The question is if you really want to use a regular expression for this. wouldn't a string operation do the job? someting like

        string blah = @"Host Name: IT786P20
        OS Name: Microsoft Windows XP Professional"
        int colonIndex = blah.IndexOf(":");
        string HostName = blah.SubString(colonIndex+1,blah.IndexOf("\r",colonIndex+1)-colonIndex+1).Trim();
        Console.WriteLine(HostName);

        btw. if you want to work with regular expressions i would recommend you to get expresso it's a free tool with a great regex-Builder and Tester. here[^] greets m@u

        L Offline
        L Offline
        lordbearsg
        wrote on last edited by
        #3

        yes i have tried using the regex-builder but i do not know what is the syntax for making a regular expression. my ultimate purpose of using regular expression is becuase i wan to use regular expression to find the text that i want from a text file. btw is there any software that allows me to type in a word to match from a sample text and gives me the regular expression syntax?

        L S 2 Replies Last reply
        0
        • L lordbearsg

          yes i have tried using the regex-builder but i do not know what is the syntax for making a regular expression. my ultimate purpose of using regular expression is becuase i wan to use regular expression to find the text that i want from a text file. btw is there any software that allows me to type in a word to match from a sample text and gives me the regular expression syntax?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          ah ok.. but you could still use String Operations for it ;) will that "Machine Name: someName" occur only once in the file? if so, there's almost no difference between doing it the String Operation - Way or with Regex.. what you mean with syntax? basically if you want to match a word this already IS a regular Expression it's no problem to say Regex r = new Regex("Machine Name:"); or in your case, to get the Machine Name "Machine Name: (?\w)". if you use it like that, after applying the regex on your string with Match() you can say string MachineName = myMatch.Groups["MachineName"].Value;

          L 1 Reply Last reply
          0
          • L lordbearsg

            yes i have tried using the regex-builder but i do not know what is the syntax for making a regular expression. my ultimate purpose of using regular expression is becuase i wan to use regular expression to find the text that i want from a text file. btw is there any software that allows me to type in a word to match from a sample text and gives me the regular expression syntax?

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

            /// using System.Text.RegularExpressions;(NameSpace for Regular Exprestion...) public static bool IsAlphaNumeric(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithSpace(String strToCheck) { Regex objAlphaNumericSpacePattern = new Regex(@"^[a-zA-Z0-9\s]*$"); return !objAlphaNumericSpacePattern.IsMatch(strToCheck); } public static bool IsNumericWithDot(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[0-9.]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsNumeric(String strToCheck) { Regex objNumericPattern = new Regex(@"^[0-9]*$"); return !objNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithDot(String strToCheck) { Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9.]"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithDotPeriodSpace(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9\s.,]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithDotPeriodSpaceHyphen(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9\s.,-]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithDotPeriodSpaceHyphenSlash(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9\s.,-/]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithSlash(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9/]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAmount(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^(\d{1,12}|(\d{1,12}\.{1}\d{1,3}){1})$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAmountBECost(String strToCheck) { Regex objAmountPattern = new Regex(@"^(\d{1,8}|(\d{1,8}\.{1}\d{1,3}){1})$"); return !objAmountPattern.IsMatch(strToCheck); }

            modified on Tuesday, December 18, 2007 4:08:49 AM

            L 1 Reply Last reply
            0
            • L Lost User

              ah ok.. but you could still use String Operations for it ;) will that "Machine Name: someName" occur only once in the file? if so, there's almost no difference between doing it the String Operation - Way or with Regex.. what you mean with syntax? basically if you want to match a word this already IS a regular Expression it's no problem to say Regex r = new Regex("Machine Name:"); or in your case, to get the Machine Name "Machine Name: (?\w)". if you use it like that, after applying the regex on your string with Match() you can say string MachineName = myMatch.Groups["MachineName"].Value;

              L Offline
              L Offline
              lordbearsg
              wrote on last edited by
              #6

              i mean syntax like this: (?sx-m)[^\r\n].*?(?:(?:\.|\?|!)\s) It gave me an error like this Error 1 No overload for method 'Regex' takes '1' arguments Sry to bother u but can u give me a complete code if i were to give u a psuedo code??

              L 1 Reply Last reply
              0
              • L lordbearsg

                i mean syntax like this: (?sx-m)[^\r\n].*?(?:(?:\.|\?|!)\s) It gave me an error like this Error 1 No overload for method 'Regex' takes '1' arguments Sry to bother u but can u give me a complete code if i were to give u a psuedo code??

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                lordbearsg wrote:

                It gave me an error like this Error 1 No overload for method 'Regex' takes '1' arguments

                hmm.. that's strange.. in .net 1.1 and 2.x it works if you only pass the pattern to the constructor. what other Parameter does it want?

                1 Reply Last reply
                0
                • S Sandilian

                  /// using System.Text.RegularExpressions;(NameSpace for Regular Exprestion...) public static bool IsAlphaNumeric(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithSpace(String strToCheck) { Regex objAlphaNumericSpacePattern = new Regex(@"^[a-zA-Z0-9\s]*$"); return !objAlphaNumericSpacePattern.IsMatch(strToCheck); } public static bool IsNumericWithDot(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[0-9.]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsNumeric(String strToCheck) { Regex objNumericPattern = new Regex(@"^[0-9]*$"); return !objNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithDot(String strToCheck) { Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9.]"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithDotPeriodSpace(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9\s.,]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithDotPeriodSpaceHyphen(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9\s.,-]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithDotPeriodSpaceHyphenSlash(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9\s.,-/]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAlphaNumericWithSlash(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9/]*$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAmount(String strToCheck) { Regex objAlphaNumericPattern = new Regex(@"^(\d{1,12}|(\d{1,12}\.{1}\d{1,3}){1})$"); return !objAlphaNumericPattern.IsMatch(strToCheck); } public static bool IsAmountBECost(String strToCheck) { Regex objAmountPattern = new Regex(@"^(\d{1,8}|(\d{1,8}\.{1}\d{1,3}){1})$"); return !objAmountPattern.IsMatch(strToCheck); }

                  modified on Tuesday, December 18, 2007 4:08:49 AM

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  None of these would work, as you are returning not IsMatch, so it will return true if its not matched. :doh: The . in some places need to be escaped too, as it will match anything, if unescaped.

                  xacc.ide
                  IronScheme a R5RS/R6RS-compliant Scheme on the DLR
                  The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                  L 1 Reply Last reply
                  0
                  • L leppie

                    None of these would work, as you are returning not IsMatch, so it will return true if its not matched. :doh: The . in some places need to be escaped too, as it will match anything, if unescaped.

                    xacc.ide
                    IronScheme a R5RS/R6RS-compliant Scheme on the DLR
                    The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                    L Offline
                    L Offline
                    lordbearsg
                    wrote on last edited by
                    #9

                    Below is how i intented on how to make the code work but how do i code it? Pseudo code class test { public void Test(some arugments??) { Read systeminfo.txt; Search for the word Host Name and show in a message box; } }

                    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