19

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
  • 71,299
  • 12
  • 93
  • 154
FirmView
  • 3,112
  • 8
  • 32
  • 47
  • 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

31

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
  • 49,835
  • 33
  • 140
  • 131
15

try:

.+

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

Robert
  • 1,009
  • 12
  • 24
B.K.
  • 181
  • 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
  • 7,776
  • 9
  • 44
  • 55
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
  • 748
  • 1
  • 13
  • 20
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
  • 11,867
  • 12
  • 36
  • 73
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
  • 841
  • 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