Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9068131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:07:03+00:00 2026-06-16T17:07:03+00:00

This feels like a really simple question, but I can’t find the answer anywhere.

  • 0

This feels like a really simple question, but I can’t find the answer anywhere.

(Notes: I’m using Python, but this shouldn’t matter.)

Say I have the following string:

s = "foo\nbar\nfood\nfoo"

I am simply trying to find a regex that will match both instances of “foo”, but not “food”, based on the fact that the “foo” in “food” is not immediately followed by either a newline or the end of the string.

This is perhaps an overly complicated way to express my question, but it gives something concrete to work with.

Here are some of the things I have tried, with results (Note: the result I want is [foo\n, foo]):

foo[\n\Z] => ['foo\n']

foo(\n\Z) => ['\n', ''] <= This seems to match the newline and EOS, but not the foo

foo($|\n) => ['\n', '']

(foo)($|\n) => [(foo,'\n'), (foo,'')] <= Almost there, and this is a useable plan B, but I would like to find the perfect solution.

The only thing I found that does work is:

foo$|foo\n => ['foo\n', `’foo’]

This is fine for such a simple example, but it is easy to see how it could become unwieldy with a much larger expression (and yes, this foo thing is a stand in for the larger expression I am actually using).


Interesting aside: The closest SO question I could find to my problem was this one: In regex, match either the end of the string or a specific character

Here, I could simply substitute \n for my ‘specific character’. Now, the accepted answer uses the regex /(&|\?)list=.*?(&|$)/. I notice that the OP was using JavaScript (question was tagged with the javascript tag), so maybe the JavaScript regex interpreter is different, but when I use the exact strings given in the question with the above regex in Python, I get bad results:

>>> findall("(&|\?)list=.*?(&|$)", "index.php?test=1&list=UL")
[('&', '')]
>>> findall("(&|\?)list=.*?(&|$)", "index.php?list=UL&more=1")
[('?', '&')]

So, I’m stumped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:07:06+00:00Added an answer on June 16, 2026 at 5:07 pm
    >>> import re
    >>> re.findall(r'foo(?:$|\n)', "foo\nbar\nfood\nfoo")
    ['foo\n', 'foo']
    

    (?:...) makes a non-capturing group.

    This works because (from re module reference):

    re.findall(pattern, string, flags=0)

    Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This feels like a really basic question, but I can't figure it out anyway..
I feel like the answer to this question is really simple, but I really
I feel like this might be really simple but I'm just not getting it
I feel like there's a simple solution to this, but I'm not really sure
This feels like it should be pretty simple, but not much seems to be
OK, this feels like an idiot question, but I'm stuck - I don't know
It feels like there must be some semi-simple solution to this, but I just
I feel kind of silly asking this question as it seems really simple, but
I want to do something that feels like it should be really simple, but
I really want to get this to work, but I feel like I've been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.