Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-line regular expressions. #4934

Merged
merged 2 commits into from
Nov 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ export
@__FILE__,
@b_str,
@r_str,
@r_mstr,
@v_str,
@mstr,
@unexpected,
Expand Down
1 change: 1 addition & 0 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ end
Regex(pattern::String) = Regex(pattern, DEFAULT_OPTS)

macro r_str(pattern, flags...) Regex(pattern, flags...) end
macro r_mstr(pattern, flags...) Regex(pattern, flags...) end

copy(r::Regex) = r

Expand Down
21 changes: 21 additions & 0 deletions doc/manual/strings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,27 @@ For example, the following regex has all three flags turned on:
julia> match(r"a+.*b+.*?d$"ism, "Goodbye,\nOh, angry,\nBad world\n")
RegexMatch("angry,\nBad world")

Sometimes you want use double-quotes in regular expressions. Escaping them can
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would shorten this to something like: "You can also use triple-quoted string literals for regular expression, which is convenient for embedding quotation marks and newlines" and then maybe give a single example.

be annoying. Now you can use tripple quotes.

.. doctest::

julia> r = r"""
"[^"]*"
"""xs
r"
\"[^\"]*\"
"sx

Triple-quoted strings have worked for a while now:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this example is needed. match takes any string for its second argument, and triple-quoted literals are just strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. My goals are:

  1. Document the existing, undocumented, feature of triple-quoted strings --
    which are good for multi-line comments, doc strings, usage strings or
    anywhere you want to quote a quote.
  2. Document the new feature of triple-quoted-regex strings.

Anyway, I will rollback and write something better.

On Tue, Nov 26, 2013 at 7:54 AM, Steven G. Johnson <[email protected]

wrote:

In doc/manual/strings.rst:

@@ -699,6 +699,27 @@ For example, the following regex has all three flags turned on:
julia> match(r"a+.b+.?d$"ism, "Goodbye,\nOh, angry,\nBad world\n")
RegexMatch("angry,\nBad world")

+Sometimes you want use double-quotes in regular expressions. Escaping them can
+be annoying. Now you can use tripple quotes.
+
+.. doctest::
+

  • julia> r = r"""
  •       "[^"]*"
    
  •       """xs
    
  • r"
  •       \"[^\"]*\"
    
  •       "sx
    
    +Triple-quoted strings have worked for a while now:

I don't think this example is needed. match takes any string for its
second argument, and triple-quoted literals are just strings.


Reply to this email directly or view it on GitHubhttps://github.com//pull/4934/files#r7928335
.

Michael

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, no. I'm going to merge this and we can edit after it gets merged. Bikeshedding code is one thing, but bikeshedding documentation is silly. We can just merge and edit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before I get to that, though -- it seem like some of this falls under
style-guide.rst. I've only been working with Julia for a few days and so
am hardly the one to be dictating style.

It seems me that in Julia, the style is to write almost no inline
documentation and instead write documents separately. This is as opposed to
Python where 80% of your typical source file is triple-quoted docstrings
and inline commentary.

The Julia advantage is that the documentation ends up reading like a
document whereas a lot of Python documentation extracted by Sphinx reads
like machine-generated goo.

The Python advantage is that it's more fun to read source code.

I don't know if this is an intentional design decision or just the way
things have worked out so far. What do people think? Pythonic? Or Julian?

On Tue, Nov 26, 2013 at 9:05 AM, Michael Fox [email protected] wrote:

Fair enough. My goals are:

  1. Document the existing, undocumented, feature of triple-quoted strings
    -- which are good for multi-line comments, doc strings, usage strings or
    anywhere you want to quote a quote.
  2. Document the new feature of triple-quoted-regex strings.

Anyway, I will rollback and write something better.

On Tue, Nov 26, 2013 at 7:54 AM, Steven G. Johnson <
[email protected]> wrote:

In doc/manual/strings.rst:

@@ -699,6 +699,27 @@ For example, the following regex has all three flags turned on:
julia> match(r"a+.b+.?d$"ism, "Goodbye,\nOh, angry,\nBad world\n")
RegexMatch("angry,\nBad world")

+Sometimes you want use double-quotes in regular expressions. Escaping them can
+be annoying. Now you can use tripple quotes.
+
+.. doctest::
+

  • julia> r = r"""
  •       "[^"]*"
    
  •       """xs
    
  • r"
  •       \"[^\"]*\"
    
  •       "sx
    
    +Triple-quoted strings have worked for a while now:

I don't think this example is needed. match takes any string for its
second argument, and triple-quoted literals are just strings.


Reply to this email directly or view it on GitHubhttps://github.com//pull/4934/files#r7928335
.

Michael

Michael

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gitfoxi, that's due purely to a technical reason: Julia doesn't currently have an analogue to Python's docstrings. See #3988.


.. doctest::

julia> match(r, """
"foo"
""")
RegexMatch("\"foo\"")

Byte Array Literals
~~~~~~~~~~~~~~~~~~~

Expand Down