# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(\*\*NOTE\*\*: )_([^_]*)_\n"
test_str = ("JavaScript run in the browser has one set of rules when run in a\n"
"browser, and another set when run outside a browser. If you are using\n"
"JavaScript primarily to write client side scripts meant to be run in a\n"
"browser, then it is best to learn JavaScript, and develop JavaScript,\n"
"under that scenario. It is true that there are legitimate and important\n"
"ways to run JavaScript from outside the browser. For instance, you can\n"
"run JavaScript from the command prompt, or directly from inside some\n"
"IDEs. At first, however, such stratagems can lead to much confusion. As\n"
"a result, I suggest that you begin by developing JavaScript inside a\n"
"browser.\n\n"
"**NOTE**: _I should probably qualify what I say above. The basic syntax\n"
"of the language does not change when you switch from a browser to some\n"
"environment. (The only exception, of course, is when a browser has a\n"
"buggy implementation of JavaScript, and that still happens quite\n"
"frequently.) But even when everything works correctly, certain key\n"
"features of the language, such as the **this** keyword, have a\n"
"different significance inside a browser and outside a browser. Also, key\n"
"elements of the API, such as the **alert** function, are available in a\n"
"browser and not outside a browser. These and other differences become\n"
"manageable when you gain proficiency in the language, but at first, it\n"
"is best to avoid such subtle pitfalls by running JavaScript in the\n"
"environment in which you intend to use it. Of course, if you are\n"
"intending to write mostly server side JavaScript with **nodejs**, then\n"
"this advice is less convincing. I don't not think there are serious\n"
"disadvantages to learning JavaScript in a browser even if you want to\n"
"use it on the server side, but you will find that there are differences.\n"
"In general, I think it is easier to move from the browser to **nodejs**,\n"
"than it is to move from **nodejs** to the quirky world of browsers._\n\n"
"It turns out that the code you saw in the previous section provides a\n"
"good framework for beginning and intermediate level JavaScript\n"
"programmers who want to learn more about the langauge. Start out by\n"
"opening up code similar to what you see in Listing 3 and 4. As a matter\n"
"of fact, you can simply reuse VerySimple.html over and over again. As we\n"
"explore the JavaScript language, all you need do is change the name of\n"
"the JavaScript file that you are linking in. For instance, linking\n"
"VerySimple01.js for one program, then VerySimple02.js for the next\n"
"program. Better yet, follow best practices and rename each JavaScript\n"
"file to reflect its contents. For instance, ExploringLoops.js would be a\n"
"good name for a JavaScript file that you created when you wanted to\n"
"learn about how loops are written in JavaScript.")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html