2

Python's re module says this:

'^'

(Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline.

I want to use MULTILINE but I want to require a match at the beginning of the string (not just the beginning of a line). Is there a way to do this?

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • DO NOT remove the context information from the title. SO suggested pages don't include tag information. – Jason S Oct 23 '15 at 22:24
  • From the "How to use tags" FAQ: The only time you should use tags in your title is when they are organic to the conversational tone of the title. Your title does not follow this guideline. If everyone does this it makes it very difficult to understand what the question is about from the title because of the extra (redundant) data in the title. – Chad S. Oct 23 '15 at 22:30
  • This has been debated on Meta (can't remember where). My position is leave the title alone. It's my question, and it's a pet peeve of mine that I waste time clicking through links because they don't tell me that it's Javascript or C# instead of Python. Until SO decides they are going to include tag information alongside the suggested links, I will continue to include context information in the titles of my questions. – Jason S Oct 23 '15 at 22:32
  • 2
    debated or no, the guidelines are pretty clear on the subject. Also the tag information is included in the general list, and in the suggested and related links are suggested and related based on (in large part) the tags. It's redundant and lowers the signal to noise ratio – Chad S. Oct 23 '15 at 22:34
  • [http://stackoverflow.com/questions/30267695/matching-only-the-beginning-anchor-of-a-multiline-string](Matching only the beginning anchor of a multiline string) is Javascript, and it's in the related list of questions shown (to me at least) on this page. – Jason S Oct 23 '15 at 22:37
  • You would prefer that the entire list started with "PYTHON:" "python:" "python---" "Help with Python:" etc? – Chad S. Oct 23 '15 at 22:38
  • I don't care how they indicate it, I just want to get some context information. And FWIW they are *GUIDELINES*. – Jason S Oct 23 '15 at 22:39
  • @JasonS given that you're talking about regex syntax, which isn't unique to Python, why would it matter if the answer was on a question tagged with a different language? This is kind of surprising behaviour for someone in your position, frankly. – jonrsharpe Oct 23 '15 at 22:40
  • Because each of the languages have their own individual implementation quirks. – Jason S Oct 23 '15 at 22:41
  • I'm getting back to work. stribizhev has done me the valuable service of answering two questions I couldn't figure out on my own; the rest of this quibbling about titles is just getting me pissed off. – Jason S Oct 23 '15 at 22:41
  • So your real issue is that the suggested and related links shouldn't cross contaminate questions from other languages. I agree with you (mostly). But that doesn't make the issues with the signal/noise in putting tags in posts justified. – Chad S. Oct 23 '15 at 22:42
  • Also fwiw the answer is the same in Ruby and probably some other languages. So in this case it might make sense to link to other languages. – Chad S. Oct 23 '15 at 22:43
  • @JasonS would have been worth finding out if that was the case first, surely? Or just searching the Python docs page for *"start of the string"*? You're the one acting like an asshole here, don't try taking the high road now you've got your answer. You might be getting back to work but your laziness wastes other people's time, too. – jonrsharpe Oct 23 '15 at 22:46
  • @JasonS: then why not focus on the things that matter and are worth getting upset about? The community has already decided this one: please keep the tags out of the title. – Martijn Pieters Oct 24 '15 at 13:14

1 Answers1

3

Just use the \A anchor that matches the start of string unambiguously.

Check the Regular Expression Syntax:

\A
Matches only at the start of the string.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • ah, cool. Somehow I missed seeing that. It would be nice if the documentation for `^` referenced the `\A` pattern. – Jason S Oct 23 '15 at 22:23
  • Right, there are a lot of such issues with the docs. Like `re.MULTILINE` won't work with `re.match`, this is also lost somewhere there. – Wiktor Stribiżew Oct 23 '15 at 22:25
  • "`re.MULTILINE` won't work with `re.match`" -- really? yikes, that's kind of important. – Jason S Oct 23 '15 at 22:25
  • Well, I might have re-worded it. It is mentioned in [`re.match`](https://docs.python.org/2/library/re.html#re.match), not in [`re.MULTILINE`](https://docs.python.org/2/library/re.html#re.M) section: *Note that even in `MULTILINE` mode, `re.match()` will only match at the beginning of the string and not at the beginning of each line.* – Wiktor Stribiżew Oct 23 '15 at 22:27
  • Oh. Maybe I should have just used `re.match` then. For some reason I thought it tried to match the entire string (anchors at both beginning and end) – Jason S Oct 23 '15 at 22:30