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

  • Home
  • SEARCH
  • 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 6695137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:11:07+00:00 2026-05-26T06:11:07+00:00

I want to find the position in a string, where a regular expression stops

  • 0

I want to find the position in a string, where a regular expression stops matching.

Simple example:

my $x = 'abcdefghijklmnopqrstuvwxyz';
$x =~ /gho/;

This example shall give me the position of the character ‘h’ because ‘h’ matches and ‘o’ is the first nonmatching character.

I thought of using pos or $- but it is not written on unsuccessful match.
Another solution would be to iteratively shorten the regex pattern until it matches but that’s very ugly and doesn’t work on complex patterns.

EDIT:

Okay for the linguists: I’m sorry for my awful explanation.

To clarify my situation: If you think of a regular expression as a finite automaton, there is a point, where the testing interrupts, because a character doesn’t fit. This point is what I’m searching for.

Use of iterative paranthesis (as mentioned by eugene y) is a nice idea, but it doesn’t work with quantifiers and I had to edit the pattern.

Are there other ideas?

  • 1 1 Answer
  • 2 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-26T06:11:08+00:00Added an answer on May 26, 2026 at 6:11 am

    What you are proposing is difficult but doable.

    If I can paraphrase what I understand, you are wanting to find out how far a failing match got into a match. In order to do this, you need to be able to parse a regex.

    The best regex parser is probably to use Perl itself with the -re=debug command line switch:

    $ perl -Mre=debug -e'"abcdefghijklmnopqr"=~/gh[ijkl]{5}/'
    Compiling REx "gh[ijkl]{5}"
    Final program:
       1: EXACT <gh> (3)
       3: CURLY {5,5} (16)
       5:   ANYOF[i-l][] (0)
      16: END (0)
    anchored "gh" at 0 (checking anchored) minlen 7 
    Guessing start of match in sv for REx "gh[ijkl]{5}" against "abcdefghijklmnopqr"
    Found anchored substr "gh" at offset 6...
    Starting position does not contradict /^/m...
    Guessed: match at offset 6
    Matching REx "gh[ijkl]{5}" against "ghijklmnopqr"
       6 <bcdef> <ghijklmnop>    |  1:EXACT <gh>(3)
       8 <defgh> <ijklmnopqr>    |  3:CURLY {5,5}(16)
                                      ANYOF[i-l][] can match 4 times out of 5...
                                      failed...
    Match failed
    Freeing REx: "gh[ijkl]{5}"
    

    You can shell out that Perl command line with your regex and parse the return of stdout. Look for the `

    Here is a matching regex:

    $ perl -Mre=debug -e'"abcdefghijklmnopqr"=~/gh[ijkl]{3}/'
    Compiling REx "gh[ijkl]{3}"
    Final program:
       1: EXACT <gh> (3)
       3: CURLY {3,3} (16)
       5:   ANYOF[i-l][] (0)
      16: END (0)
    anchored "gh" at 0 (checking anchored) minlen 5 
    Guessing start of match in sv for REx "gh[ijkl]{3}" against "abcdefghijklmnopqr"
    Found anchored substr "gh" at offset 6...
    Starting position does not contradict /^/m...
    Guessed: match at offset 6
    Matching REx "gh[ijkl]{3}" against "ghijklmnopqr"
       6 <bcdef> <ghijklmnop>    |  1:EXACT <gh>(3)
       8 <defgh> <ijklmnopqr>    |  3:CURLY {3,3}(16)
                                      ANYOF[i-l][] can match 3 times out of 3...
      11 <ghijk> <lmnopqr>       | 16:  END(0)
    Match successful!
    Freeing REx: "gh[ijkl]{3}"
    

    You will need to build a parser that can handle the return from the Perl re debugger. The left hand and right hand angle braces show the distance into the string as the regex engine is trying to match.

    This is not an easy project btw…

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

Sidebar

Related Questions

I have a string and i want to find the position of all the
I want to find position in( position within string, but I can not explicitly
I want to find the position of a substring in a string if present
Lets say I have around 1,000,000 users. I want to find out what position
I want to find a sql command or something that can do this where
HI, I want to find all input elements in a HTML string that has
I'm using strpos to find the position of a string in another string. I
Say I have a string text, a caret position caret and then want to
I want my search results to be in order of string position from smallest
How to find the position or location of string in given document.I have one

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.