• Login
Community
  • Login

Find and Replace specific character in a specific string

Scheduled Pinned Locked Moved General Discussion
replacefindregex
7 Posts 2 Posters 12.2k 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.
  • S
    Saeed Ghazi Joolaee
    last edited by Jan 4, 2016, 10:17 AM

    Hi,
    I have a text:


    way 150841940
    highway = residential
    name = ‘Smith 120’
    name:fr = ‘Simon 120’
    nd 1637110349
    nd 1637110360


    Then I want to replace any digit in lines which have "name = " in it with another character, and I don’t want any digit in other places be replaced.

    for example, I want replace 1 with X then :
    name = ‘Smith 120’ --------> name = ‘Smith X20’
    but name:fr = ‘Simon 120’ --------> no change
    and nd 1637110349-------->no change

    C 1 Reply Last reply Jan 4, 2016, 4:07 PM Reply Quote 0
    • C
      Claudia Frank @Saeed Ghazi Joolaee
      last edited by Jan 4, 2016, 4:07 PM

      Hello Saeed-Ghazi-Joolaee,

      from the provided example I assume that Smith 120 is surrounded by a single quote.

      Open replace dialog and check regular expression in search mode.
      The regex which needs to be used is

       (.*name = ')(.* )(\d+)(')
      

      which should result in 4 groups

      \1: name = '
      \2: Smith 
      \3: 120
      \4: '
      

      what means the replace dialog needs to be used like

      Find what    :(.*name = ')(.* )(\d+)(')
      Replace with :\1\2xxx\4
      

      which would then result in

      way 150841940
      highway = residential
      name = 'Smith xxx'
      name:fr = 'Simon 120'
      nd 1637110349
      nd 1637110360
      

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 0
      • S
        Saeed Ghazi Joolaee
        last edited by Jan 4, 2016, 10:04 PM

        Thanks Claudia,

        But I mean something else. I have numerouse sequence of this text block with different “names” which have different numbers in them. I want to replace “1” in all names with “X”, '2" with “Y”, and “3” with “Z” …9

        way 35103881
        name = ‘4/2’
        nd 411824630
        nd 717069654

        way 152126825
        name = ‘2nd Street’
        nd 1649547187
        nd 1649547183
        nd 1649547177

        way 191544328
        name = ‘Joe 618’
        nd 2021290076
        nd 2021290075
        nd 2021290074
        nd 2021290073

        way 191544409
        name = ‘John 13’
        nd 2021290720
        nd 2021290719

        (Excuse me for long Example)

        How can I replace one by one digit with my desired letters in “name” lines. Maybe with help of Marked lines this could be done, isn’t it?

        C 1 Reply Last reply Jan 5, 2016, 12:49 AM Reply Quote 0
        • C
          Claudia Frank @Saeed Ghazi Joolaee
          last edited by Claudia Frank Jan 5, 2016, 12:49 AM Jan 5, 2016, 12:49 AM

          Hello Saeed-Ghazi-Joolaee

          still having problems to understand the logic which needs to be used.
          I understand only the line with name = is of interested.
          Can we safely assume that the value which needs to be changed is surrounded by single quotes?
          Is the first digit,which needs to be replaced, strictly mapped to a letter ?
          The provided example

          name = ‘4/2’ needs to be changed to something like name=‘Y/2’
          and
          name = ‘Joe 618’ to something like name = ‘Joe R18’

          Correct?

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 0
          • S
            Saeed Ghazi Joolaee
            last edited by Jan 5, 2016, 7:40 AM

            Hello Claudia,

            Thanks for your contribution,
            Exactly, I want to replace Latin numbers with Persian numbers. For your convenience, I used here Latin letters.

            I must do it digit by digit, because I don’t know which numbers is used in my text (I can’t do it from 0 to 9999999999…, Thus, It’s better done digit by digit from 0 to 9). And I want only to replace numbers in name = lines not other numbers - which must be in English intact.

            @ Can we safely assume that the value which needs to be changed is surrounded by single quotes?
            quotes are not important at all.

            @ name = ‘Joe 618’ to something like name = ‘Joe R18’
            Correct?

            Exactly. Finally after replacing all 9 digits I want name = ‘Joe RAV’

            Thanks in advance.

            C 1 Reply Last reply Jan 5, 2016, 7:45 PM Reply Quote 0
            • C
              Claudia Frank @Saeed Ghazi Joolaee
              last edited by Jan 5, 2016, 7:45 PM

              Hello @Saeed-Ghazi-Joolaee

              quotes are not important at all.
              they are important as I use a regualr expression based on the existance of the single quotes.

              You need to run the regex 10 times, for each digit one run.
              You build 4 groups ( ), where as group 3 is the number you are looking for
              Something like

              (.*name = ')(.*)(8)(.*')
              

              and replace with

              \1\2V\4
              

              The letter which is used as replacement is between group \2 and \4 without spaces.

              I hope this does what you are looking for.

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 1
              • S
                Saeed Ghazi Joolaee
                last edited by Jan 6, 2016, 2:01 PM

                Hi Claudia,

                Thanks for your kind help. I almost reached my destination. The only thing is that for numbers which have equal digits like 33 or 333 I must search two or three times to complete my replacement. As far as my numbers are maximum 3 digit numbers, this in no matter.

                Cheers.

                1 Reply Last reply Reply Quote 0
                4 out of 7
                • First post
                  4/7
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors