jkwhite
jkwhite

Reputation: 320

Using regex to capture everything between two asterisks

I'm exporting several issues from JIRA and just like SO, they utilize asterisks to deal with formatting. I'm looking to keep that formatting in when put into a word document however, there are several times where the asterisks are providing me with issues. I currently have a regular expression that works to get text between two asterisks:

(\*.+?\*)

This regular expression will match a significant set of the items that I'm looking at:

*This is a bolded sentence*. This is not. *This is again.*

Matching:

This is a bolded sentence
This is again

However, when I want to check a sentence as follows:

*Set the dropdown to (*.docx) and click 'Save'*. Do the next thing.

It will only match:

Set the dropdown to (

Is there a way that I can match based on what follows? For example, if there is an asterisk period and space or a way to specify avoiding an asterisk period letter then it would match:

Set the dropdown to (*.docx) and click 'Save'

I have tried modifying the current regex that I have but cannot seem to find something that works. Even when using a negated sets [^*.] and negative look-ahead (?!\*\.).

Upvotes: 1

Views: 1550

Answers (1)

anubhava
anubhava

Reputation: 784998

You may use this regex with a negative lookahead after closing * to fail the match when we have a dot and non-space character after *:

\*.*?\*(?!\.\S)

RegEx Demo

  • (?!\.\S) is a negative lookahead to assert failure when there is a dot followed by non-whitespace character right after closing *

Upvotes: 1

Related Questions