Don't match only spaces - regex

I have the following regexp that matches underscores, letters, numbers, and spaces. My problem is that I don't want to match strings with only spaces. Any help? Thanks!
[a-zA-Z0-9_\\s]+

If the first character must be a non-space then this will do:
[a-zA-Z0-9_][a-zA-Z0-9_\s]*
If you need to be able to match leading spaces as well:
\s*[a-zA-Z0-9_][a-zA-Z0-9_\s]*

[a-zA-Z0-9_\\s]*[a-zA-Z0-9_]+[a-zA-Z0-9\\s]*
There are more elegant ways to phrase this, but I don't know if your tool of choice supports it.

Related

How can I replace all special characters in a cell except for spaces and dashes in Google Sheets using regex?

I'm having trouble using a regex expression to replace all special characters in a cell except for spaces and dashes and was wondering if someone could help.
I'm trying different variations and I'm able to do the following.
Replace all special characters, but words like "what's" turn into "what s ".
Replace all non-word characters but getting no spaces between words.
If anyone could help here that would be great. I just want my regex expression to replace all special characters in a cell except for spaces and dashes. If it can be done without a regex formula that would be great too. Thanks a bunch in advance for all the help!
It would be easier to set the special characters you want to remove/replace since you can control which ones are the characters to be removed.
Formula:
=REGEXREPLACE(A1, "[!##$%^&*()]", "")
Sample:
Or set the characters only allowed. (replace all characters that are not numbers, letters, space and dash)
Formula:
=REGEXREPLACE(A1, "[^0-9a-zA-Z -]", "")
Sample:
Reference:
For the syntax google is using, see link
Just use this: =REGEXREPLACE(A1, "[^a-zA-Z0-9\-\s]", "")
That will remove all characters that aren't a letter, a digit, a dash or a space.
Example: https://docs.google.com/spreadsheets/d/1LLL9hKdm035fikv96KjP3sAZTIcKs4GqBuxWHHsVO7g/edit?usp=sharing

Special regex rule for wordlist

How do I create a proper regex rule to the following rules?
Must contain at least one number.
Must contain at least one alphabetic character a-z.
No more than 4 of the same number or letter in a row.
Any ideas? Thank you!
Use lookaheads and back referencing:
^(?=.*[a-z])(?=.*\d)(?!.*(.)\1{3}).*$
Regex101
Edit: If you do not wish to match strings that have white space characters, you can do:
^(?=.*[a-z])(?=.*\d)(?!.*(.)\1{3})\S*$ // replaced the . at the end with \S

Regular expression spaces in words

Given:
<currency: word with spaces, 56>
and the regular expression:
<(?:CURRENCY):[ ]*(\w+(\s*,\s*)\d+(\s*\d+)*)>
What must I change to accept spaces in the "words with spaces"
You're currently searching for \w which is a word character, the opposite of whitespace. Also, not sure if you're intending to capture a whitespace and commas, instead of the number values. This captures only the word and the numbers.
<CURRENCY:\s+?(.+)\s*,\s*(\d+)(?:\s*(\d+))?>
I find regex101.com to be helpful when debugging these things.
does this help?
<(?:currency)\:\s*\w(\w|\s)+,\s*\d+(\s*|\d+)*>
This should do:
<currency: ((\w+\s*)*), (\d+)>
another one:
<(?:currency:)\s([\w\s]*),\s\d+>
if don't want to capture empty string, change * to +
Also you don't need the non capturing group ?:
<currency:\s([\w\s]*),\s\d+>
would do the same.
This worked for me:
<(currency):[ ]*(\w+(\s+\w+)*\s*,\s*\d+(\s*\d+)*)>
Not sure what "?:" does, so you may want:
<(?:CURRENCY):[ ]*(\w+(\s+\w+)*\s*,\s*\d+(\s*\d+)*)>

Which regex allows me to match characters and digits from specific String like GIVEN_CHAR_VAL":"AKRONIS387226279863_NXUS0000000016092126"

Which regex allows me to match characters and digits from String GIVEN_CHAR_VAL":"AKRONIS387226279863_NXUS0000000016092126"
I tried
GIVEN_CHAR_VAL":"(.*)"
but doesn't work correct.
Any ideas?
If you only want to match alphanumeric characters, use \w rather than .:
GIVEN_CHAR_VAL":"(\w*)"
Your suggested regex works for me, but have you tried:
GIVEN_CHAR_VAL":"(.*?)"
What do you actually want to match?
.* will give you the entire set AKRONIS387226279863_NXUS0000000016092126
\w+ as suggested above will do the same because it accepts '_'
If you are trying to match everything except the underscore try something more specific like [A-Z0-9]+ though you will end up with two matches because of the intervening underscore.

Regular expression to allow certain characters and disable a few others

I have to write a regular expression which allows alphabets and numbers, but does not permit characters like / and ? . Can someone help me out on this? Thanks in advance
This regex does what you have asked for:
^(?!.*(/|\?))[a-zA-Z0-9]+$
It simply matches only on input that is comprised solely of letters and/or numbers, plus it does a negative lookahead to make sure no / or ? are present.
To exclude more special chars, just add them to the look ahead, for example to also exclude # use this (?!.*(/|\?|#)) (the ? needs escaping with a backslash \)
Assuming you want to allow more than just ASCII letters and numbers (and your regex flavor supports Unicode), you can use
^[\p{L}\p{N}]*$
you could try to concentrate on characters that you dont want to allow if that list is more clear than allowed one:
^[^\?\/\s\(\)]+$
Fill [] with more characters that you dont like
The meta character \w matches alphanumerics which means all numbers, ASCII letters and underscore (_).