How to detect dot (.), underscore(_) and dash(-) in regex [closed]












1














I need to process information dealing with IP address or folders containing information about an IP host.



I need a pattern that can identify (match) IP addresses, whether an actual url, name of folder or data file . For example



127.0.0.10
127-0-0-10
127_0_0_10


should all match. above. Is there any tokenizer regex to do this in bash?










share|improve this question













closed as unclear what you're asking by Michael Homer, RalfFriedl, Jeff Schaller, roaima, JigglyNaga Dec 21 '18 at 10:50


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 4




    Matching IP addresses with regex is no easy task. How accurate should your match be? Should it fail for an address of 0.0.0.0 or 10.10.10.256?
    – Jesse_b
    Dec 20 '18 at 19:42






  • 4




    how are you matching? with wildcards? with grep? with a bash [[ =~ ]] test?
    – Jeff Schaller
    Dec 20 '18 at 19:49










  • IPv4 or IPv6? Or both?
    – roaima
    Dec 20 '18 at 22:16










  • "I need a pattern that can identify (match) IP addresses, whether an actual url, name of folder or data file". Pretty much the only thing that will match all these requirements is a full wildcard .*, at which point you might as well abandon any idea of matching with REs. Or did you mean to match just the IP address component within those different types of string? Please be precise when describing what you need. Ideally provide examples of the different matches required.
    – roaima
    Dec 20 '18 at 22:19








  • 1




    127-0-0-10 isn't standard notation for an IPv4 address. Is this some sort of exception to the pattern matching requirement?
    – roaima
    Dec 20 '18 at 22:22
















1














I need to process information dealing with IP address or folders containing information about an IP host.



I need a pattern that can identify (match) IP addresses, whether an actual url, name of folder or data file . For example



127.0.0.10
127-0-0-10
127_0_0_10


should all match. above. Is there any tokenizer regex to do this in bash?










share|improve this question













closed as unclear what you're asking by Michael Homer, RalfFriedl, Jeff Schaller, roaima, JigglyNaga Dec 21 '18 at 10:50


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 4




    Matching IP addresses with regex is no easy task. How accurate should your match be? Should it fail for an address of 0.0.0.0 or 10.10.10.256?
    – Jesse_b
    Dec 20 '18 at 19:42






  • 4




    how are you matching? with wildcards? with grep? with a bash [[ =~ ]] test?
    – Jeff Schaller
    Dec 20 '18 at 19:49










  • IPv4 or IPv6? Or both?
    – roaima
    Dec 20 '18 at 22:16










  • "I need a pattern that can identify (match) IP addresses, whether an actual url, name of folder or data file". Pretty much the only thing that will match all these requirements is a full wildcard .*, at which point you might as well abandon any idea of matching with REs. Or did you mean to match just the IP address component within those different types of string? Please be precise when describing what you need. Ideally provide examples of the different matches required.
    – roaima
    Dec 20 '18 at 22:19








  • 1




    127-0-0-10 isn't standard notation for an IPv4 address. Is this some sort of exception to the pattern matching requirement?
    – roaima
    Dec 20 '18 at 22:22














1












1








1







I need to process information dealing with IP address or folders containing information about an IP host.



I need a pattern that can identify (match) IP addresses, whether an actual url, name of folder or data file . For example



127.0.0.10
127-0-0-10
127_0_0_10


should all match. above. Is there any tokenizer regex to do this in bash?










share|improve this question













I need to process information dealing with IP address or folders containing information about an IP host.



I need a pattern that can identify (match) IP addresses, whether an actual url, name of folder or data file . For example



127.0.0.10
127-0-0-10
127_0_0_10


should all match. above. Is there any tokenizer regex to do this in bash?







bash regular-expression ip-address






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 20 '18 at 19:35









Nederealm

1112




1112




closed as unclear what you're asking by Michael Homer, RalfFriedl, Jeff Schaller, roaima, JigglyNaga Dec 21 '18 at 10:50


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Michael Homer, RalfFriedl, Jeff Schaller, roaima, JigglyNaga Dec 21 '18 at 10:50


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 4




    Matching IP addresses with regex is no easy task. How accurate should your match be? Should it fail for an address of 0.0.0.0 or 10.10.10.256?
    – Jesse_b
    Dec 20 '18 at 19:42






  • 4




    how are you matching? with wildcards? with grep? with a bash [[ =~ ]] test?
    – Jeff Schaller
    Dec 20 '18 at 19:49










  • IPv4 or IPv6? Or both?
    – roaima
    Dec 20 '18 at 22:16










  • "I need a pattern that can identify (match) IP addresses, whether an actual url, name of folder or data file". Pretty much the only thing that will match all these requirements is a full wildcard .*, at which point you might as well abandon any idea of matching with REs. Or did you mean to match just the IP address component within those different types of string? Please be precise when describing what you need. Ideally provide examples of the different matches required.
    – roaima
    Dec 20 '18 at 22:19








  • 1




    127-0-0-10 isn't standard notation for an IPv4 address. Is this some sort of exception to the pattern matching requirement?
    – roaima
    Dec 20 '18 at 22:22














  • 4




    Matching IP addresses with regex is no easy task. How accurate should your match be? Should it fail for an address of 0.0.0.0 or 10.10.10.256?
    – Jesse_b
    Dec 20 '18 at 19:42






  • 4




    how are you matching? with wildcards? with grep? with a bash [[ =~ ]] test?
    – Jeff Schaller
    Dec 20 '18 at 19:49










  • IPv4 or IPv6? Or both?
    – roaima
    Dec 20 '18 at 22:16










  • "I need a pattern that can identify (match) IP addresses, whether an actual url, name of folder or data file". Pretty much the only thing that will match all these requirements is a full wildcard .*, at which point you might as well abandon any idea of matching with REs. Or did you mean to match just the IP address component within those different types of string? Please be precise when describing what you need. Ideally provide examples of the different matches required.
    – roaima
    Dec 20 '18 at 22:19








  • 1




    127-0-0-10 isn't standard notation for an IPv4 address. Is this some sort of exception to the pattern matching requirement?
    – roaima
    Dec 20 '18 at 22:22








4




4




Matching IP addresses with regex is no easy task. How accurate should your match be? Should it fail for an address of 0.0.0.0 or 10.10.10.256?
– Jesse_b
Dec 20 '18 at 19:42




Matching IP addresses with regex is no easy task. How accurate should your match be? Should it fail for an address of 0.0.0.0 or 10.10.10.256?
– Jesse_b
Dec 20 '18 at 19:42




4




4




how are you matching? with wildcards? with grep? with a bash [[ =~ ]] test?
– Jeff Schaller
Dec 20 '18 at 19:49




how are you matching? with wildcards? with grep? with a bash [[ =~ ]] test?
– Jeff Schaller
Dec 20 '18 at 19:49












IPv4 or IPv6? Or both?
– roaima
Dec 20 '18 at 22:16




IPv4 or IPv6? Or both?
– roaima
Dec 20 '18 at 22:16












"I need a pattern that can identify (match) IP addresses, whether an actual url, name of folder or data file". Pretty much the only thing that will match all these requirements is a full wildcard .*, at which point you might as well abandon any idea of matching with REs. Or did you mean to match just the IP address component within those different types of string? Please be precise when describing what you need. Ideally provide examples of the different matches required.
– roaima
Dec 20 '18 at 22:19






"I need a pattern that can identify (match) IP addresses, whether an actual url, name of folder or data file". Pretty much the only thing that will match all these requirements is a full wildcard .*, at which point you might as well abandon any idea of matching with REs. Or did you mean to match just the IP address component within those different types of string? Please be precise when describing what you need. Ideally provide examples of the different matches required.
– roaima
Dec 20 '18 at 22:19






1




1




127-0-0-10 isn't standard notation for an IPv4 address. Is this some sort of exception to the pattern matching requirement?
– roaima
Dec 20 '18 at 22:22




127-0-0-10 isn't standard notation for an IPv4 address. Is this some sort of exception to the pattern matching requirement?
– roaima
Dec 20 '18 at 22:22










1 Answer
1






active

oldest

votes


















2














The following regex seems to accomplish what you need:



b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])b




$ cat test.txt
127.0.0.10
127-0-0-10
127_0_0_10
256_5_10_1
10-10-100-1
192.168.100.1
$ grep -E 'b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])b' test.txt
127.0.0.10
127-0-0-10
127_0_0_10
10-10-100-1
192.168.100.1




See https://www.regular-expressions.info/ip.html for an explanation of the regex. I have simply modified the .s with [-._] so that it will match -, ., or _.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    The following regex seems to accomplish what you need:



    b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])b




    $ cat test.txt
    127.0.0.10
    127-0-0-10
    127_0_0_10
    256_5_10_1
    10-10-100-1
    192.168.100.1
    $ grep -E 'b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])b' test.txt
    127.0.0.10
    127-0-0-10
    127_0_0_10
    10-10-100-1
    192.168.100.1




    See https://www.regular-expressions.info/ip.html for an explanation of the regex. I have simply modified the .s with [-._] so that it will match -, ., or _.






    share|improve this answer


























      2














      The following regex seems to accomplish what you need:



      b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])b




      $ cat test.txt
      127.0.0.10
      127-0-0-10
      127_0_0_10
      256_5_10_1
      10-10-100-1
      192.168.100.1
      $ grep -E 'b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])b' test.txt
      127.0.0.10
      127-0-0-10
      127_0_0_10
      10-10-100-1
      192.168.100.1




      See https://www.regular-expressions.info/ip.html for an explanation of the regex. I have simply modified the .s with [-._] so that it will match -, ., or _.






      share|improve this answer
























        2












        2








        2






        The following regex seems to accomplish what you need:



        b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])b




        $ cat test.txt
        127.0.0.10
        127-0-0-10
        127_0_0_10
        256_5_10_1
        10-10-100-1
        192.168.100.1
        $ grep -E 'b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])b' test.txt
        127.0.0.10
        127-0-0-10
        127_0_0_10
        10-10-100-1
        192.168.100.1




        See https://www.regular-expressions.info/ip.html for an explanation of the regex. I have simply modified the .s with [-._] so that it will match -, ., or _.






        share|improve this answer












        The following regex seems to accomplish what you need:



        b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])b




        $ cat test.txt
        127.0.0.10
        127-0-0-10
        127_0_0_10
        256_5_10_1
        10-10-100-1
        192.168.100.1
        $ grep -E 'b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[-._](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])b' test.txt
        127.0.0.10
        127-0-0-10
        127_0_0_10
        10-10-100-1
        192.168.100.1




        See https://www.regular-expressions.info/ip.html for an explanation of the regex. I have simply modified the .s with [-._] so that it will match -, ., or _.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 20 '18 at 19:51









        Jesse_b

        11.9k23064




        11.9k23064















            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre