Editing in Notepad++

  • Hi,

    how we can insert single quotes in a perticular word


    input-- Adelanto 15515 361 363
    output-- ‘Adelanto’ 15515 361 363

  • welcome to the notepad++ community @Inshita-Rawat

    first make sure you use a copy of your desired file and only use the copy for testing the things below.

    1. if the word is always Adelanto, you can do it by using replace (you can find replace in the search menu of notepad++):

    find what: ^(.*?)(Adelanto)(.*?)$
    replace with: $1'$2'$3
    search mode: Regular Expression
    (important. Regular Expression has to be selected as search mode)

    then press replace all

    1. if the word Adelanto can be anything but is always at the beginning, you can use this:

    find what: ^(.*?) (.*?)$
    replace with: '$1'$2
    search mode: Regular Expression

    then press replace all

    it is best to copy and paste all “find what” and “replace with” texts, marked in red above, to your search and replace fields in notepad++ as they are very sensitive for any missing or different characters

    if your real life text file is more complex, for example if Adelanto can also be 2 or more words like Adelanto San Bernardino, or the position of it varies, we will need a sample of your original content.

  • Thank you so much @Meta-Chuh
    this solution worked but later i came to know that i have data like

    input-- Adelanto Hill 15515 361 363
    output-- ‘Adelanto Hill’ 15515 361 363

    and can you please explain me the code so that i can make my own…

  • Do you mean normal quotes or smart quotes? What did you try when you saw there was a slight difference?

    Since the only difference I can see between your original data
    input-- Adelanto 15515 361 363
    output-- 'Adelanto' 15515 361 363
    and your new data
    input-- Adelanto Hill 15515 361 363
    output-- 'Adelanto Hill' 15515 361 363
    … is the word “Hill”, did you try to edit the Find What to be ^(.*?)(Adelanto Hill)(.*?)$

    Here’s some boilerplate, with links to regex help, and to a post that shows how to format example text in such a way as the forum doesn’t edit it (for example, your post looked like ‘smart quotes’, but @meta-chuh assumed ‘normal quotes’ instead.)

    FYI: if you have further search-and-replace (regex) needs, study this FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting the data so that the forum doesn’t mangle it (so that it shows “exactly”, as I said earlier), see this help-with-markdown post, where @Scott-Sumner gives a great summary of how to use Markdown for this forum’s needs.
    Please note that for all “regex” queries – or queries where you want help “matching” or “marking” or “bookmarking” a certain pattern, which amounts to the same thing – it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match.

  • In the whole data set i have few words like
    Adelanto 155533
    Anaheim 155222
    Agoura Hills 15999
    Apple Valley 158888…
    so i cannot use any specific word and change it one by one as they are in thousands. So i want to learn code for that so that in one go we can take care of that

  • @Inshita-Rawat

    as @Peter-Jones mentioned, you only have to replace Adelanto in
    find what: ^(.*?)(Adelanto)(.*?)$ to the text you need for the second pass, so that it will look like this
    find what: ^(.*?)(Adelanto Hill)(.*?)$
    and replace with: $1’$2’$3

    little explanation:
    ^ this is the symbol for regex (regular expression) to tell it that all following parameters assume that it has to start at the beginning of a line

    (.*?) tells regex that all characters found after the beginning of the line and before any other will be saved to $1.
    the () brackets will create a string buffer (a place to store a section of text or numbers) and store its containing or found value or string as string 1, which can later be accessed by either using $1 or \1.
    .*? is the symbol combination for “ungreedy anything” to make sure the string can match anything, regardless of it is a number, a special character except a line ending, a letter or any combination of those.

    (Adelanto Hill) is the string you are searching for. the brackets again create a new string buffer that can be accessed later by writing $2 in your replace field

    next but almost invisible comes a single space, which marks that after Adelanto Hill there has to be a space between the text you are searching for and the beginning of the number.

    (.*?) again tells regex that all characters found after the beginning of the line and before any other will be saved to a new string buffer, $3.

    $ is the symbol for regex to stop the previous commands (expressions) once an end of a line has been found, so that it will reset it’s buffers and discontinue the search if you press replace once. this allows regex to handle every line by line, instead of the whole document.

    the replace with sequence: $1’$2’$3 then just takes the 3 string buffers and put them together with a ’ as separator

    for a more detailed information and a more sophisticated regex search, that could for example enquote any text before a number under more complex circumstances, we will need the help of for example our regex guru @guy038 as i am only “first level” regarding regex ;-)

  • If you want to learn the code, read the resources I linked to above. If you want our help, please format your data, as I linked above. Show some effort, and we’ll be quite willing to help.

    If you had told us that “Adelanto” was not the exact text you were looking for, but rather any text before some number or sequence of numbers, then we could have crafted the right regex from the beginning.

    Assuming your rule is “find a line that has text, followed by a space, followed by any combination of numbers and spaces, I want to grab the text before the numbers, and put single-quotes around them”

    Adelanto 155515 361 363
    Anaheim 155222 123 456
    Agoura Hills 15999 876
    Apple Valley 158888
    • Find What = ^(.*?) ([\d\h]+)
    • Replace = '$1' $2
    • mode = regular expression

    will result in

    'Adelanto' 155515 361 363
    'Anaheim' 155222 123 456
    'Agoura Hills' 15999 876
    'Apple Valley' 158888

    The full explanation can be found in the FAQ I already linked, but in short:

    • Find What = ^(.*?) ([\d\h]+)
      • ^ = match beginning of line
      • () = put what’s inside into a numbered match: there will be two numbered matches, based on the two pairs of parentheses in the regex
      • . = match any character
      • .* = match any character, zero or more times
      • .*? = match any character, zero or more times, non-greedily – this will gobble up all the text at the beginning of the line that isn’t explicitly matched later in the regex
      • the space between the sets of parens () () is a literal space
      • [] = match any single character listed inside the brackets
      • \d = match any digit (0-9)
      • \h = match any horizontal space (space or tab character)
      • thus, [\d\h] = match any of the numbers or spaces mentioned
      • + = do the previous match one or more times.
      • [\d\h]+ = match any sequence of one or more digits or spaces
      • $ = match the end of the line
    • Replace = '$1' $2
      • replace the above with the first numbered match in single quotes, space, and the second numbered match

    So the full regular expression says “grab everything up to the first space that’s followed by one or more digits or spaces resulting in end of line, into two groups”, and the replace will put quotes around the first group.

    This won’t match on data like

    Some Text Here 555 123 4567 xyz

    because the xyz is not composed of only numbers and spaces.
    If you want to be able to match this too, then the rule would be “match start of line and a bunch of characters, stopping at the first digit after a space, and put the beginning text into quotes”, it could be done with ^(.*?) (\d.*)$. Or, if you took the $ away from my first regex => ^(.*?) ([\d\h]+), then it just won’t require an end-of-line immediately after the numbers-and-spaces, so any other text is allowed there, too.

    There are lots of other ways to accomplish it, too.

    Study the documents I linked.

    (Ah, @Meta-Chuh did a great explanation of the existing regexes, which I saw just before hitting submit. Since I already typed this up, and since this goes into details on how to change it based on your changing data requirements, I will still post.)

  • thank you @Peter-Jones and @Meta-Chuh for explaining me of each code in notepad++ in a clear way as it is really difficult o find explanation of codes in internet which can help in making codes of problems.

  • You’re welcome, @Inshita-Rawat. If you follow the links in the FAQ, especially to one of the try-it-live websites, like regexr.com (*), they will explain the individual elements of the regular expression in great detail.

    *: I usually use regexr, and switch it from “JAVASCRIPT” to “PCRE” in the upper right. PCRE comes come close to Notepad++'s Boost-based regular expressions — since both Boost and PCRE are based on Perl regular expressions, though there are differences (both from each other, and from Perl).

    If you paste my ^(.*?) (\d.*) regular expression into regexr.com with PCRE, and FLAGS=global,multiline (/gm), along with example data – see https://regexr.com/466oe – you can see the explanation, or by clicking on REPLACE, you can input my replace string, and see what it will become.

Log in to reply