-1

I have already found similar question but unfortunately it does not fit to my requirements -> regular expression for anything but an empty string

Question: Is this possible to match with Regex anything or nothing but not string which contains only whitespaces?

String s1 = "";       // -> true
String s2 = "test";   // -> true
String s3 = "  test"; // -> true
String s5 = " ";      // -> false
String s6 = "   ";    // -> false
Dawid
  • 347
  • 2
  • 15
  • 1
    You'd have to actually write a regex expression, which you have not even attempted to do. – Scott Hunter Jan 03 '22 at 14:13
  • @ScottHunter Of course, I tried, but what would you like to see here, my every single regex which I tried to write and which does not work for my example? – Dawid Jan 03 '22 at 14:16
  • There is a vast difference between posting *everything* you tried and not posting *anything*. For example, if you posted one thing you tried and explained how it fell short, someone might be able to show you how to fix it. – Scott Hunter Jan 03 '22 at 14:17
  • [`"\t"` is only whitespace](https://rubular.com/r/V1viyi55Tm9NNn). Are you sure this should match? – Bohemian Jan 03 '22 at 14:20
  • Try `^($|.*\S.*)$` – Bohemian Jan 03 '22 at 14:26
  • @Bohemian after typing `\t` it should be matched as string and not as "empty spaces", or better to use double escaped symbols `\\t` ? – Dawid Jan 03 '22 at 14:37
  • @dawid your question does not use the language “empty spaces”; it says *contains only whitespaces*. Your example String s4 is a single tab character, which is a “whitespace” character - ie it is matched by the regex `\s`, so why is it marked “true” in your examples? – Bohemian Jan 03 '22 at 14:43
  • @Bohemian regex from @Wiktor works as intended for every my example. I though that, `\t` will be recognized as "whitespaces" but it works correctly. Important info, regex is working in Angular frontend (Ignite UI) layer so it's possible that form handles that different from Java code. – Dawid Jan 03 '22 at 14:48
  • @Dawid what do you mean *I thought that `\t` will be recognized as "whitespaces"*? It IS a whitespace. Please edit your question to be consistent: Either all-whitespace strings should not match OR `"\t"` should match. Both cannot be true. – Bohemian Jan 03 '22 at 14:53
  • 1
    As shown by [this demo](https://rubular.com/r/wSlSxU1g4FHgRH), example string `s4` is NOT matched by Wiktor’s regex, but your question says it should match. Please clarify. – Bohemian Jan 03 '22 at 14:56
  • @Bohemian I think, it will be even better if I remove `s4` from examples list, because as I see, Angular is converting everything in form to String, even if I write `\t` in input form, it is recognized as string "\t" and not "whitespaces" so regex is working correctly. – Dawid Jan 03 '22 at 14:57
  • @dawid `"\t"` is a string containing exactly 1 character and that character is a tab character (ASCII 9). – Bohemian Jan 03 '22 at 14:59
  • **Show us what you’ve done so far** so that we have something to start with. It’s much easier for us to help you with existing code, and people are far more likely to help when they don’t have to start from zero with "How do I do this?" – Andy Lester Jan 03 '22 at 18:02

2 Answers2

1

You can use

^(?!\s+$).*

See the regex demo (changed to PCRE and \h to match just horizontal whitespaces for the sake of the demo (\h is not supported in JavaScript regex)).

See a JavaScript snippet showcasing how the regex works:

const trues = ["", "test", "  test"];
const falses = [" ", "\t"];
const regex = /^(?!\s+$).*/;
console.log(trues.every(x => regex.test(x)));   // true, all true cases are matched
console.log(falses.every(x => !regex.test(x))); // true, all false cases failed to match
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

This can solve your issues:

/\s/g