0

I have searched alot but didn't find my specific case. I need a regex to match everything except what's written between two angle brackets:

I found how to match everything except angle brackets: String regex="[^<>]*";

Or how to match the content between two angle brackets: \<(.*?)\> or <([^>]+)>

Which is fine however I need the excact opposite of that. I tried playing around with ^ (negate) but didn't have sucess.

For example: Fara Foo <not be selected>;another <also not be selected>

should return: Fara Foo <>;another <>

The whole thing should work in java.

Update: A replaceAll(...) solution does not help me since I want to use the regex in an replaceAll(...) call :-) So I really need the regex.

Since it was asked in a comment: In java there there are certain string operations like replaceAll() or split() which directly take a regex. There is another way by using Pattern and Matcher. Its more convenient to use replaceAll() instead of the pattern matcher. Thats why I want to use a "negative" regex to be able to use replaceAll...

Lonzak
  • 9,334
  • 5
  • 57
  • 88
  • Just split with the `<[^<>]*>` pattern – Wiktor Stribiżew Apr 14 '20 at 14:21
  • @WiktorStribiżew OP don't want to select what's wrapped in <> – Animesh Sahu Apr 14 '20 at 14:23
  • 1
    Couldn't you just do a `str.replaceAll("<([^>]+)>", "");' to strip out the text you want to exclude? – simon-pearson Apr 14 '20 at 14:26
  • `str = str.replaceAll("<[^>]*>", "<>");` should work. – anubhava Apr 14 '20 at 14:28
  • @AnimeshSahu When you split, you do not "select" anything, you get chunks of text that do not match the regex used to split the text. – Wiktor Stribiżew Apr 14 '20 at 14:36
  • Please explain "*I want to apply the regex to an replaceAll(...) call*". – Wiktor Stribiżew Apr 14 '20 at 14:55
  • 1
    Do you mean this regex: `(^.+?<)|(>.+?<)|(>.+$)` which return `Fara Foo <>;another <>; ...` ? – S.R. Apr 14 '20 at 15:19
  • Lonzak, please edit your question to include 1) a sample string (you have `Fara Foo ;another ; ...`, but it would be nice to make sure it is a full sample string), 2) exact expected result with the type of result (string, array, etc.) and why that result and not some other one is expected. – Wiktor Stribiżew Apr 14 '20 at 15:41
  • @S.R. Thanks that one is working! I you post this as an anwer I'll except it – Lonzak Apr 14 '20 at 16:12
  • How come it works? If you have two or more `<...>` substrings it will put the second, third, etc. into the third capturing group. How are you planning to use it? – Wiktor Stribiżew Apr 14 '20 at 16:16
  • @WiktorStribiżew Ok here is what I want to do: I want to use the replaceAll() method in java and not Pattern/Matcher. That is why I need an inverse regex (to replace everything else outside <> with ""). Now simon-pearson wrote me a solution by using the replaceAll() method which gives me the result however is not usable for me for the above reason... – Lonzak Apr 14 '20 at 16:36
  • *to replace everything else outside <> with ""* - You mean you need to remove all but `<...>`? Or wrap with quotes? Also, [my splitting approach showcase](https://ideone.com/bJ8GYi). – Wiktor Stribiżew Apr 14 '20 at 16:52
  • Yes I need to remove all but <...>. I disagree on your closing since the linked post doesn't solve this question. – Lonzak Apr 14 '20 at 17:31
  • It does solve your *problem*, it does not and will not give you the whole solution, as what you ask for is not how regexps work. The workaround is shown in my answer there, just scroll to the *a sequence of characters:* section. – Wiktor Stribiżew Apr 15 '20 at 08:33

2 Answers2

0

One way to achieve the desired results would be to use string.replaceAll to strip the content between angle brackets out, e.g.:

String strIn = "Fara Foo <not be selected>;another <also not be selected>; ...";
String newStr = strIn.replaceAll("<([^>]+)>", "<>");
simon-pearson
  • 1,601
  • 8
  • 10
  • 2
    If you would replace it with "<>" it would even work however I really need the regex solution because I want to apply the call to a `replaceAll`call. So unfortunatly your solution doesn't help me... – Lonzak Apr 14 '20 at 14:44
  • My initial post was wrong, I didn't realise you wanted to keep the angle brackets. I don't see how the above doesn't achieve what you want? – simon-pearson Apr 14 '20 at 15:27
  • I want to use the regex in an replaceAll(...) call myself so your answer was correct but wasn't for me since I can't put your solution in an replaceAll() call... But I already added that as an clarification... – Lonzak Apr 14 '20 at 17:33
0

You can use (?<=<) to look after < and (?=>) to look behind > and by that ignoring both open and closing angle brackets:

String input = "Fara Foo <not be selected>;another <also not be selected>; ...";
String regex = "(?<=<)([^>]*)(?=>)";
input = input.replaceAll(regex, "");

OUTPUTS:
Fara Foo <>;another <>; ...
Bahij.Mik
  • 1,358
  • 2
  • 9
  • 22