• trevor@lemmy.mlOP
    link
    fedilink
    arrow-up
    5
    ·
    2 months ago

    Here is an example. Given the following list of strings:

    1.2.3.4
    127.0.0.1
    127.0.0.2
    127.0.0.11
    128.0.0.1
    125.0.0.*
    200.0.0.1
    2001:0db8:85a3:0000:0000:8a2e:0370:1300
    2001:0db8:85a3:0000:0000:8a2e:0370:1330
    2001:0db8:85a3:0000:0000:8a2e:0370:1337
    あいうえお
    あいうえこ
    😃😁😆😐
    😃😁😆
    😃😁🤣
    

    list2regexp will return the following pattern:

    ^(?:1(?:(?:\.(?:2(?:\.(?:3(?:\.(?:4))))))|(?:2(?:(?:5(?:\.(?:0(?:\.(?:0(?:\.(?:.*)))))))|(?:7(?:\.(?:0(?:\.(?:0(?:\.(?:(?:1(?:|(?:1)))|(?:2))))))))|(?:8(?:\.(?:0(?:\.(?:0(?:\.(?:1)))))))))))|(?:2(?:0(?:0(?:(?:\.(?:0(?:\.(?:0(?:\.(?:1))))))|(?:1(?::(?:0(?:d(?:b(?:8(?::(?:8(?:5(?:a(?:3(?::(?:0(?:0(?:0(?:0(?::(?:0(?:0(?:0(?:0(?::(?:8(?:a(?:2(?:e(?::(?:0(?:3(?:7(?:0(?::(?:1(?:3(?:(?:0(?:0))|(?:3(?:(?:0|7))))))))))))))))))))))))))))))))))))))))))|(?:あ(?:い(?:う(?:え(?:(?:お|こ))))))|(?:😃(?:😁(?:(?:😆(?:|(?:😐)))|(?:🤣))))$
    
    • leanleft@lemmy.ml
      link
      fedilink
      English
      arrow-up
      2
      ·
      2 months ago

      it should(could) have newlines for readability. and is genererally an underappreciated feature that leads to people thinking that regex is crazy and unreadable.

      • trevor@lemmy.mlOP
        link
        fedilink
        arrow-up
        1
        ·
        2 months ago

        I didn’t know about this so I tried testing it out. Yes, this is possible, but requires enabling a specific flag (x when it comes to regexp2) assuming the engine even supports this feature.

        • leanleft@lemmy.ml
          link
          fedilink
          English
          arrow-up
          2
          ·
          2 months ago

          also comments. which can be very simple…like metadata about boundary,membership,grouping, or hierarchy.
          its supported in most languages except javascript and plain grep.
          but it doesnt serve a programmatic use… so even if its a hella useful single char or digit, that massively boosts readability… some people would argue that it’s extra/junk