Java RegEx to match substring delimited by variable number of asterisks when the asterisk is not at beginning or end of line

Viewed 35

I'm stuck on something I must be missing on a strange regex with a negative/positive look behind that should work extracting substrings delimiters made by at least 2 contiguous asterisks, with the caveat that the 2 or more asterisks should not be neither at the start nor at the end of the line of text.

Let me make an example. From something like this:

*** DRIVER FEE -10.00/DAY - SPOUSE ALSO CHARGED ****QUALIFICATIONS FOR RENTERS FROM NON US COUNTRIES*** -MUST HAVE A *** VALID DRIVERS LICENSE***

I should capture only the asterisks series in the middle, so those before QUALIFICATIONS, those following COUNTRIES and those before VALID

I should not capture asterisks before DRIVER and after LICENSE, using the example above.

I could sort out:

\b([**]+)\B

and

\B([**]+)\b

but these don't work to exclude those at the beginning and end of text.

Any idea on how to make the capture?

1 Answers

You can use

(?<=[^*])\*{2,}(?=[^*])

See the regex demo.

Details:

  • (?<=[^*]) - a positive lookbehind that requires a * char immediately on the left
  • \*{2,} - two or more asterisk chars
  • (?=[^*]) - a positive lookahead that requires a * char immediately on the right.
Related