Community
    • Login

    how to put parentheses in the first three numbers?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 967 Views
    Loading More Posts
    • 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.
    • Hibachi SushiyaH
      Hibachi Sushiya
      last edited by

      i have a set numbers like 1234567890
      how to put parentheses in the first three numbers?
      result should be (123)4567890
      thanks

      1 Reply Last reply Reply Quote 0
      • Terry RT
        Terry R
        last edited by

        @Hibachi-Sushiya said:

        parentheses in the first three numbers?

        My solution will take any number which is anywhere on a line and is at least 3 digits long.

        The regular expression is:
        Find What:([^0-9])(\d{3})(\d+)*
        Replace With:\1\(\2\)\3
        So the search mode is ‘regular expression’ and tick ‘wrap around’.

        The following is what I used for testing

        website 345678
        1234 trevor3456 never
        always 7826242
          123
        

        and the regex produces

        website (345)678
        (123)4 trevor(345)6 never
        always (782)6242
         (123)
        

        See if that helps. If an issue then you will need to expand on where the issue occurs and if there are other situations where you do (or not) want the numbers bracketed.

        Terry

        1 Reply Last reply Reply Quote 3
        • PeterJonesP
          PeterJones
          last edited by

          @Terry-R said:

          ([^0-9])(\d{3})(\d+)*

          With data like,

          4321 trevor3456 never
          website 345678
          1234 trevor3456 never
          always 7826242
            123
          

          It will miss the first number “4321”, because there is no newline before it. Changing to (^|[^0-9])(\d{3})(\d+)* will fix it: the ^|[^0-9] says “match the beginning of a line, or a non-digit character”.

          1 Reply Last reply Reply Quote 2
          • Terry RT
            Terry R
            last edited by Terry R

            @PeterJones said:

            With data like

            Thanks Peter, I was trying to cover as many variations as possible, forgot the ‘very first’ character one. It got me to thinking if perhaps I might have over complicated it a bit.

            A couple of minutes work with your ‘data like’ example I got all the required numbers using:
            Find What:(\d{3})(\d+)*
            Replace With:\(\1\)\2

            (432)1 trevor(345)6 never
            website (345)678
            (123)4 trevor(345)6 never
            always (782)6242
              (123)
            

            It seems I don’t need to force the regex to check for a ‘word boundary’ (change between digit and ‘other character’) as it does it itself with the remainder of the regex. So just discarding the first option does not appear to change the outcome.

            Cheers
            Terry

            1 Reply Last reply Reply Quote 2
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors