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. Regular Expression ?

Regular Expression ?

Scheduled Pinned Locked Moved C#
regexcomhelptutorialquestion
7 Posts 3 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.
  • J Offline
    J Offline
    Jacobb Michael
    wrote on last edited by
    #1

    Hi all Can some one lead me for a good logic to generate the regular expression dynamically. Example: I give an input like bellow 1. Us phone no - (910)456-8970 => regular expression is: @"\(\d{3}\)\s\d{3}-\d{4}" 2. Email Id - miltoncse00@yahoo.com => string pattern=@"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" + @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" + @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$"; My Need, By getting (910)456-8970 this string i want to construct the regular expression like @"\(\d{3}\)\s\d{3}-\d{4}" and i have to do for all the string vice versa. Can some tell me some techniques or logic to incorporate. My Real Problem With Regular Expression : I have to mask the fields in website based on the pattern like usphone,emailid etc... thanks in Adavance

    L 1 Reply Last reply
    0
    • J Jacobb Michael

      Hi all Can some one lead me for a good logic to generate the regular expression dynamically. Example: I give an input like bellow 1. Us phone no - (910)456-8970 => regular expression is: @"\(\d{3}\)\s\d{3}-\d{4}" 2. Email Id - miltoncse00@yahoo.com => string pattern=@"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" + @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" + @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$"; My Need, By getting (910)456-8970 this string i want to construct the regular expression like @"\(\d{3}\)\s\d{3}-\d{4}" and i have to do for all the string vice versa. Can some tell me some techniques or logic to incorporate. My Real Problem With Regular Expression : I have to mask the fields in website based on the pattern like usphone,emailid etc... thanks in Adavance

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

      I doubt that you can (correctly) deduce a regex from sample data. Take the phone-number column as an example, with this data;

      555-423325

      What mask would the first three five's generate, and would that mask be reusable as a phone-number-mask? It gets worse, since the generated mask isn't a phone-number-mask (even if you base it on the data in that column) - it will be a mask that allows all data within that column (since that's what it's based on). The best approach would be to make a list of examples for each column, containing correct and incorrect values. Next, you base the regex on the domain of the values that you expect. Lastly, a unit-test to verify that the regex works :)

      I are Troll :)

      J 1 Reply Last reply
      0
      • L Lost User

        I doubt that you can (correctly) deduce a regex from sample data. Take the phone-number column as an example, with this data;

        555-423325

        What mask would the first three five's generate, and would that mask be reusable as a phone-number-mask? It gets worse, since the generated mask isn't a phone-number-mask (even if you base it on the data in that column) - it will be a mask that allows all data within that column (since that's what it's based on). The best approach would be to make a list of examples for each column, containing correct and incorrect values. Next, you base the regex on the domain of the values that you expect. Lastly, a unit-test to verify that the regex works :)

        I are Troll :)

        J Offline
        J Offline
        Jacobb Michael
        wrote on last edited by
        #3

        Hi thanks for your reply, The mask is not only for a corporate sites, it is for the entire web. if i specify to mask all the security No (SSN) in a page, even if i Google it also, i have to mask the SSN in the Google page as (XXXXXXXX). I have done that using ihtmlElementCollection (BHO). My problem is i know SSN format so there is no issue.But there can be any format, so just seeing one format dynamically i want to generate the regex. This question may be quiet meaningless, but we can could arrive a solution. if no one had come across like this, then i will try to generate a algorithm to achieve, but my thinking is, if already some one knows on that, then i don't want to wast my time to creating algorithm again. thanks any suggestions are greatfull Thanks

        L 1 Reply Last reply
        0
        • J Jacobb Michael

          Hi thanks for your reply, The mask is not only for a corporate sites, it is for the entire web. if i specify to mask all the security No (SSN) in a page, even if i Google it also, i have to mask the SSN in the Google page as (XXXXXXXX). I have done that using ihtmlElementCollection (BHO). My problem is i know SSN format so there is no issue.But there can be any format, so just seeing one format dynamically i want to generate the regex. This question may be quiet meaningless, but we can could arrive a solution. if no one had come across like this, then i will try to generate a algorithm to achieve, but my thinking is, if already some one knows on that, then i don't want to wast my time to creating algorithm again. thanks any suggestions are greatfull Thanks

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

          jasome wrote:

          But there can be any format, so just seeing one format dynamically i want to generate the regex.

          Your application encounters the string "8572398947". Is that my SSN (being Dutch), or my SSN + 1, or just a random number? How would you know that the string is in fact, a SSN if you cannot verify that it is?

          I are Troll :)

          J 1 Reply Last reply
          0
          • L Lost User

            jasome wrote:

            But there can be any format, so just seeing one format dynamically i want to generate the regex.

            Your application encounters the string "8572398947". Is that my SSN (being Dutch), or my SSN + 1, or just a random number? How would you know that the string is in fact, a SSN if you cannot verify that it is?

            I are Troll :)

            J Offline
            J Offline
            Jacobb Michael
            wrote on last edited by
            #5

            Hi, Good exactly you are correct, sorry for taking the SSN no instead take USPHone No like ... etc.. added to that for SSN there is option to mask based on the Field value, for example if the input/span element contain SSN then i mask that. This regex is only for unique analyzing for example if i give : user@gmail.com - we can make regular expression for all the mails, Thanks for your continuous encouragement you are most welcome to make a comment on that....

            J L 2 Replies Last reply
            0
            • J Jacobb Michael

              Hi, Good exactly you are correct, sorry for taking the SSN no instead take USPHone No like ... etc.. added to that for SSN there is option to mask based on the Field value, for example if the input/span element contain SSN then i mask that. This regex is only for unique analyzing for example if i give : user@gmail.com - we can make regular expression for all the mails, Thanks for your continuous encouragement you are most welcome to make a comment on that....

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              jasome wrote:

              for example if i give : user@gmail.com - we can make regular expression for all the mails,

              So taking this example, you take that input and generate an "email address" regex from it. Now I try to enter my email address (which is .co.uk) and whoops, it fails. Yet mine is a totally valid email address. There is absolutely no way you can make a regular expression from input data and have it work in all cases.

              1 Reply Last reply
              0
              • J Jacobb Michael

                Hi, Good exactly you are correct, sorry for taking the SSN no instead take USPHone No like ... etc.. added to that for SSN there is option to mask based on the Field value, for example if the input/span element contain SSN then i mask that. This regex is only for unique analyzing for example if i give : user@gmail.com - we can make regular expression for all the mails, Thanks for your continuous encouragement you are most welcome to make a comment on that....

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

                jasome wrote:

                user@gmail.com - we can make regular expression for all the mails,

                That's assuming that the computer can recognize this as a valid email-address. The fastest validation would be done over a regex. (That's recursion, right there) In other words; you don't know if the data is valid - thus any regex that's derived from it would be a good fit for the sample data, but not for the mask that you want.

                I are Troll :)

                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