Salviati
Salviati

Reputation: 778

Match multiple conditions?

What I am trying to do is, if I have a text like this: "this is an example 23*45 and 34". I want to get "23*45" and "34". I have no problem finding ether "23*45" or "34" but having problem getting both of them at the same time.. This is what I got now:

"(-?)([0-9]+)\*(-?)([0-9]+)"

Upvotes: 0

Views: 50

Answers (1)

The fourth bird
The fourth bird

Reputation: 163207

To get "23*45" and "34", you might use:

\d+(?:\*\d+)?

Explanation

  • Match one or more digits \d+
  • A non capturing group (?:
    • Which will match an asterix and one or more digits\*\d+
  • close non capturing group )
  • Make the non capturing group optional ?

Upvotes: 3

Related Questions