guest856
guest856

Reputation: 181

Regex for string containing no whitespace and at least one '*'

I'm kind of stuck on this one. Examples of strings that should match:

Examples of strings that should not match:

(PS: I'm working in Java if that makes a difference)

Upvotes: 0

Views: 55

Answers (2)

blhsing
blhsing

Reputation: 106768

You can use a positive lookahead pattern to ensure that there is at least one * in the match, and a negative lookahead pattern to ensure that the * is not alone:

(?=\S*\*)(?!\*(?:\s|$))\S+

Demo: https://regex101.com/r/0sdl5a/1

Upvotes: 2

Patrick Parker
Patrick Parker

Reputation: 4959

Solution without using lookaheads:

[a-z]+\*[*a-z]*|\*+[a-z][*a-z]*

first case is where the required letter appears before the first star.

second case is where the star(s) appear first followed by the required letter.

the two cases are combined using |

Upvotes: 1

Related Questions