21

A line has to be validated through regex,

  1. line can contain any characters, spaces, digits, floats.

  2. line should not be blank

I tried this:

[A-Za-z0-9~`!#$%^&*()_+-]+ //thinking of all the characters

Any alternative solution will be helpful

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
FirmView
  • 3,130
  • 8
  • 34
  • 50
  • 4
    So you're saying that absolutely anything is allowed, as long as the line isn't empty? Why do you even want to use a regex for this, as opposed to the `length` function that I'm sure your language has? – ruakh Jul 23 '12 at 19:37
  • 2
    do you need regex for this? why not just trim the line and check if it is not an empty string? – jay c. Jul 23 '12 at 19:38
  • 1
    For `2. line should not be blank` -- should a line containing only whitespace be counted as "blank" or not? It makes a difference to the regex or to non-regex methods. – Stephen P Jul 23 '12 at 20:23
  • I believe this answer will help you. Good luck! http://stackoverflow.com/questions/3085539/regular-expression-for-anything-but-an-empty-string-in-c-sharp – Slavik Meltser Jul 14 '13 at 11:23

10 Answers10

32

Try this to match a line that contains more than just whitespace

/.*\S.*/

This means

/ = delimiter
.* = zero or more of anything but newline
\S = anything except a whitespace (newline, tab, space)

so you get
match anything but newline + something not whitespace + anything but newline

if whitespace only line counts as not whitespace, then replace the rule with /.+/, which will match 1 or more of anything.

Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143
  • Still works in 2023! I used this variation for search and replace in **Notepad++**: Find: `(.*) \S`, REPLACE WITH: `- \1`. This adds a dash and space in front of every non blank line essentially converting the items to a bullet list for **markdown** like in SO – Eric Hepperle - CodeSlayer2010 Aug 03 '23 at 12:57
18

try:

.+

The . matches any character, and the plus requires least one.

Robert
  • 1,357
  • 15
  • 26
B.K.
  • 211
  • 1
  • 6
  • 7
    -1, sorry. In most regex engines, `[.]` will only match an actual period. You mean `.`. – ruakh Jul 23 '12 at 19:38
7

Try : [^()]

In python with re.match() :

  >>> re.match( r"[^()]", '' )
  >>> re.match( r"[^()]", ' ' )
  <_sre.SRE_Match object at 0x100486168>
Zulu
  • 8,765
  • 9
  • 49
  • 56
3

You could just check if the line matches ^$ if it does then it's blank and you can use that as a failure, otherwise it will pass.

hkothari
  • 254
  • 3
  • 14
3

Try this:

^.+$

I used this in python BeautifulSoup when trying to find tags which do not have an attribute that is empty. It worked well. Example is below:

# get first 'a' tag in the html content where 'href' attribute is not empty
parsed_content.find("a", {"href":re.compile("^.+$")})
John
  • 768
  • 1
  • 13
  • 21
1

try

^[A-Za-z0-9,-_.\s]+$ 

this string will return true for alphabets, numbers and ,-_. but will not accept an empty string.

+ -> Quantifier, Matches between 1 and unlimited.

* -> Quantifier, Matches between 0 and unlimited.

xskxzr
  • 12,442
  • 12
  • 37
  • 77
0

This one will match everything but NOT BLANK string:

^(\s|\S)*(\S)+(\s|\S)*$

Blank string is those containing empty characters only (tabs, spaces etc.).

HannaY
  • 951
  • 1
  • 6
  • 6
0

This one will match every line with at least 1 character:

(.*?(\n))
roelvdboom
  • 11
  • 4
0
^\S+[^ ]$

^ - beginning of line

\S - any non-whitespace character

+ - one or more occurence

[^ ] - character not in the set (in this case only space), there is a space between ^ and ] this will match dsadasd^adsadas

$ - end of line

Something to help you

xrayder
  • 71
  • 1
  • 3
0

.* - matches between zero and unlimited times any character (except for line terminators)

\S - matches any non-whitespace character

Answer: .*[\S].*

'aaaaa' match

'aaaaa ' match

' aaaaa' match

' aaaaa ' match

'aaaaa aaaaa aaaaa' match

' aaaaa aaaaa aaaaa' match

'aaaaa aaaaa aaaaa ' match

' aaaaa aaaaa aaaaa ' match

' ' does not match

You can test this regular expression at: https://regex101.com

Davi Lago
  • 50
  • 4