Trying to figure out regular expressions. How to check a string, given the length of at least 1 character (not counting spaces from the beginning)? Those. a string can contain absolutely any character; the length of the string is> = 1 (but the space (s) is not considered at the beginning). ^ [a-zA-Z] {1,} $ - check for letters, at least 1x ... Which expression can replace [a-zA-Z] for accepting any characters, but as already said without a space in the beginning? Or do you have to list all the expressions (letters, numbers and special characters) in []? Yes, and spaces inside the string are valid

    3 answers 3

    Like this

    ^[^\x20] 

    you can check that at the beginning of the line ^ character was different from the space [^\x20] ( \x20 is just the space code, and ^ in the enum [] is a negation).

      \S stands for any non-whitespace character (the space itself, tabulation, carriage transfer are considered as whitespace).

      If you need exactly "NOT a space", then you can write like this [^ ]

      And if you do not set strict conditions at the beginning and end of the line, i.e. so that the whole regular session is \S (without ^ and $ ), then it will just work when it finds the first non-blank.

      /\S.*/ - will find everything from the first non space to the end of the line

      • Thanks, but how can I define the conditions for the beginning of the line? So that spaces are not initially taken into account, but only after any non-space character? - Ruben
      • @Ruben Do you need to read them or what to do with them? - Mike
      • @Ruben I wrote how. In fact, with such a question, it is not necessary to mark the beginning of the line. The regular \S will work as soon as it finds the first non-space and further .* Captures everything to the end of the line (because it tries to capture as much as possible while the condition is met, and . Denotes everything except the carriage return). Unless of course you stipulate something extra - Mike

      Maybe so:

       ^\s*\S+ 

      \ s - whitespace \ S - not whitespace