Skip to main content
dublove
Legend
May 20, 2025
Answered

I've been trying to juxtapose \Z's situation with \r, and have not been able to do so.

  • May 20, 2025
  • 1 reply
  • 303 views

For example, in the one below, I'm trying to find all the numbers at the end of the paragraph, and the numbers at the end of the article.

(\d+(?=\r))|(\d+(?=\Z))

 Individually it works, separately it fails.
Is there a way to fix this?
Is there a way that a character followed by null means it's at the very end.

 

Must it be written as two sentences?
Sometimes it's inconvenient.

Correct answer dublove

Hi Joel Cherney

Thank you very much.

 

Somehow:
\d+(? =($)|(~()) is working!
But
\d+(?=[$~(]) is implausible.

1 reply

Joel Cherney
Community Expert
Community Expert
May 20, 2025

I wasn't able to get \Z to work unless it was by itself; that seems odd, and very likely a bug.

 

That being said:

 

1) I found lots of ways that (someRegex)|(someOtherRegex) worked just fine, so long as neither query had \Z in it. No, you  usually don't have to run two separate queries. It's just when \Z is involved that the conditional "|" doesn't seem to work; neither side matches. 

 

2) I found that $ doesn't have that limitation; it matched both end-of-paragraph and end-of-story. So you could use 

\d+(?=$)

to find all digits at the end of any paragraph or story.

 

 

dublove
dubloveAuthorCorrect answer
Legend
May 21, 2025

Hi Joel Cherney

Thank you very much.

 

Somehow:
\d+(? =($)|(~()) is working!
But
\d+(?=[$~(]) is implausible.

Joel Cherney
Community Expert
Community Expert
May 21, 2025

Sorry, I can't tell what either of those means. What are you trying to capture with ~(?

 

I suspect that the regex parser fails on your second query when it tries to parse the [] inclusion group, because it can't tell what ~( means. That could mean that the first one might work because a|b is not processed in the same way as [ab] but I honestly have no idea what is going on in the parser. 

 

\d+(? =($)|(~())

\d+          one or more digits
(?=          begin positive lookahead
     ($)     location: end of paragraph 
     |       or
     (~()    what's this mean? 
)            end positive lookahead

 

\d+(?=[$~(])

\d+          one or more digits
(?=          begin positive lookahead
     [       inclusion (any character in this group)
     $       location: end of paragraph 
     ~(      what's this mean? 
)            end positive lookahead

 

If you haven't tried it already, you might want to try some kind of regex validator, like regex101.com