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 1046601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:10:16+00:00 2026-05-16T16:10:16+00:00

I’m trying to figure out the reason behind some regex comparison results I’m getting

  • 0

I’m trying to figure out the reason behind some regex comparison results I’m getting in Vim. I’m trying to match strings that begin line with one or more asterisks. Here’s how various regex’s match the strings:

echo '* text is here' =~ '\^*\*\s'  prints 1 (i.e., MATCH)
echo '* text is here' =~ '^*\*\s'   prints 0 (NO MATCH)

echo '** text is here' =~ '\^*\*\s' (MATCH)
echo '** text is here' =~ '^*\*\s'  (MATCH)

echo '*** text is here' =~ '\^*\*\s' (MATCH)
echo '*** text is here' =~ '^*\*\s'  (NO MATCH)

echo 'text is here' =~ '\^*\*\s' (NO MATCH)
echo 'text is here' =~ '^*\*\s'  (NO MATCH)

echo '*text is here' =~ '\^*\*\s' (NO MATCH)
echo '*text is here' =~ '^*\*\s'  (NO MATCH)

From these results I gather that when the begin of line character (^) is not prepended with a backslash the following * is read as a literal and the backslash_* is also read as a literal. So the result when comparing using no-initial-backslash method matches only string with exactly two asterisks followed by a whitespace.

When the ^-character is prepended with a backslash the first asterisk is a literal asterisk and the backslash-* stands for ‘zero or more of preceding character’.

The version with the initial backslash gives me the answers I want; i.e., it matches all-and-only lines beginning with one or more asterisks followed by a whitespace. Why is this? When I look at the Vim documentation it says that \^ stands for a literal ^, not the beginning of a line. I’m sure there’s a simple explanation but I can’t see it. Thanks for any clarification.

I also notice some similar behavior when typing in this question. That is, the following string has a backslash before the second asterisk that doesn’t show up in the text: ‘^**\s’ .

UPDATE: Okay, I think I’ve grokked Ross’ answer and see that the de-anchoring was giving me result I wanted. The de-anchoring is also giving me a result I don’t want, namely:

echo 'text* is here' =~ '\^*\*\s' (MATCH)

SO MY QUESTION NOW IS: what regex will match all-and-only lines that begin with one or more asterisks followed by a whitespace? The regex below gets close but fails on the final example:

echo '*** text is here' =~ '^**\s' (MATCH)
echo '* text is here' =~ '^**\s' (MATCH)
echo 'text* is here' =~ '^**\s' (NO MATCH)
echo ' * text is here' =~ '^**\s' (MATCH) -- want a no match here

The version with slash-asterisk as first asterisk doesn’t work either (i.e., ‘^\**\s’ ).

FINAL UPDATE: Okay, I think I found the version that works. I don’t understand exactly why it works, though. It looks like what I would expect except for the asterisk after the ^ character, but having a repeater after the ^ seems nonsensical:

echo '*** text is here' =~ '^*\**\s' (MATCH)
echo '* text is here' =~ '^*\**\s'   (MATCH)
echo 'text* is here' =~ '^*\**\s'   (NO MATCH)
echo ' * text is here' =~ '^*\**\s' (NO MATCH)
  • 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-05-16T16:10:17+00:00Added an answer on May 16, 2026 at 4:10 pm

    Ahh, interesting explanation, but not quite right.

    The \^ indeed refers to a literal circumflex.

    But * doesn’t mean “one or more”, it means “zero or more”, so \^* simply matches nothing if it needs to in order to make the rest of the expression succeed, and in addition it obviously will “deanchor” the rest of the search making it easier to succeed.

    I imagine that with this piece of the puzzle filled in you will have no trouble understanding the rest…

    Update: I think the final piece of the puzzle is that vi does something a bit different with out-of-context regex magic characters. If you use one in a context where it can’t be magic, you won’t get an error like you might with Perl or Ruby, the character simply becomes non-magic. And * doesn’t repeat the ^ anchor, so a search like /*/ or /^*/ will look for any actual * or a line beginning with an actual *, respectively.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is

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.