0

I have two regular expressions that work fine to extract text between characters:

  1. (?<=\$)(.*)(?=\*)
  2. (?<=\$)(.*)(?=)

For my example text $66* the first expression extracts 66. When the asterisk is not present in the text (i.e. $66), the second expression extracts 66.

How can I combine the two to use the first one if an asterisk is present and the second one if no asterisk is present?

I tried with what I thought would be an if|then|else like below but am doing something wrong: (?(?=\*)(?<=\$)(.*)(?=\*)|(?<=\$)(.*)(?=))

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93

2 Answers2

2

You can use a negated character set to exclude asterisks in your match instead:

(?<=\$)[^*]+

Demo: https://regex101.com/r/vuGBiJ/2

blhsing
  • 91,368
  • 6
  • 71
  • 106
0

As you are already using a capture group, you could also match the $ and capture 1+ characters except the asterix.

\$([^*]+)

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70