how to put parentheses in the first three numbers?



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



  • @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



  • @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”.



  • @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


Log in to reply