-1

I hope you can help me with the following problem.

One of following example strings may be given:

aaa
<bbb>
aaa<bbb>
<bbb>aaa
aaa<bbb><ccc>
<bbb>aaa<ccc>
<bbb><ccc>aaa

I'm searching for a regular expression that matches to any of those possibilities:

aaa
<bbb>
<ccc>

What I've learned so far: With <.*?> I can match the parts with the brackets, but the string "aaa" bothers me and I don't get it how to make this also a separate match.

Do you guys have any idea?

Thank you in advance for your appreciated help!

Best regards,

Rafael

r-kane
  • 29
  • 2
  • It appears you wish to parse XML or HTML with regular expressions. That approach has [unfortunate effects](http://stackoverflow.com/a/1732454/67392). Have you considered using an XML or HTML parser instead? – Richard Jul 27 '15 at 09:28
  • 1
    Hello Richard, It's not really html/xml, I'm trying to parse. It's just a string, that can have normal text or a notation for commands which are inside the brackets. And depending on what's inside the string I want to call separate functions. – r-kane Jul 27 '15 at 09:32
  • Could you provide a *real* example string and add a language tag to your question? – Casimir et Hippolyte Jul 27 '15 at 09:57
  • Hello Casimir. Here is an example string: `Is this my string? Yeah, this is my String!` – r-kane Jul 27 '15 at 10:08

1 Answers1

0

Could just add alternation. See test at regex101:

<[^>]*>|[^<]+

Also changed .*? to [^>]* as of better performance and no worries about using s flag.

Further use capturing groups to separate matches if desired.

Jonny 5
  • 12,171
  • 2
  • 25
  • 42