I have a situation where using VBScript I need to check for the presence of multiple spaces.
I want to check for the presence of 2 or more consecutive spaces, so \s+ doesnt work for my needs.
Does anyone know how I can accomplish this using VBScript regular expressions.
Use brackets to specify how many repetitions to match. This matches two or more whitespace characters:
\s{2,}
If you want to match only space characters, just use a space instead of \s, or the character code:
\x20{2,}
This ought to do the trick:
\s{2,}
Related
I match multiple spaces and some special chars with the following regex:
(?:\s{2,}|\!|\#|\#|\$|\€|\£|\%|\^|\&|\*|\(|\)|\,|\.|\?|\"|\:|\{|\}|\||\<|\>|\\)
Is it possible to shorten this expression in any way? Maybe somebody see a variant without a need to divide chars with a pipe?
Strange, that this expression (\s{2,}|[!##$€£%^&*(),.?":{}|<>§\\]) works in https://www.regextester.com/, but not in https://regexr.com/
You can use the | and a character class together like this:
/(?:\s{2,}|[!##$€£%\^&\*\(\),\.?"\:\{\}\|\<\>\\§])/
I also removed most of the backslashes as only a few of the characters need to be escaped.
I have this line
pattern = "\S*\w+(\s?$|\s{1,}\w+)+"
It all works fine as it allows me to block the initial white space, and allow at those between the words, but I can not include special characters (for example: '+' &%) without changing this property. Can someone help me out ? Thank you
If all you want is a space split you should replace \w with \S.
And anyway having \S*\w+ is sort of redundant, you could simplify with \S*\w.
But if you want finer control why not write out the whole range and replace \w with [a-zA-Z0-9_+&%]?
Check out regular expressions for javascript
Only \S matches special characters, \w only matches [a-zA-Z0-9_].
So you could simply replace them to
pattern="\S*\S+(\s?$|\s{1,}\S+)+"
but there is so much redundancy then. Simplify it to
pattern="\S+(\s+\S+)*\s?"
or if really the only thing you care about is starting with \S then just do
pattern="\S[\s\S]*" <!-- or -->
pattern="\S.*" <!-- not allowing linebreaks -->
From my understanding if you want it to not find the whitespaces or the special characters simply remove the \S* this matches anything OTHER then whitespace which includes special characters.
\w+(\s?$|\s{1,}\w+)+
This means it would block the whitespace and the special characters at the beginning of the regex however special characters inbetween words would be ignored. for that i would replace the \s with \W for non-word characters. This would allow spaces and special characters in between the words.
\w+(\W?$|\W{1,}\w+)+
A great site to test out regex and where I was able to confirm this was regex101.com it's a place you can test out the regex as you type it with detailed information that displays what your regex will do as you type it. You can also include sample text to see what your regex will find in the text. the above regex when given: " ! Test" only captured the Test and ignored both the ! and the spaces prior to Test.
I have this regular expression
([A-Z], )*
which should match something like
test, (with a space after the comma)
How to I change the regex expression so that if there are any characters after the space then it doesn't match.
For example if I had:
test, test
I'm looking to do something similar to
([A-Z], ~[A-Z])*
Cheers
Use the following regular expression:
^[A-Za-z]*, $
Explanation:
^ matches the start of the string.
[A-Za-z]* matches 0 or more letters (case-insensitive) -- replace * with + to require 1 or more letters.
, matches a comma followed by a space.
$ matches the end of the string, so if there's anything after the comma and space then the match will fail.
As has been mentioned, you should specify which language you're using when you ask a Regex question, since there are many different varieties that have their own idiosyncrasies.
^([A-Z]+, )?$
The difference between mine and Donut is that he will match , and fail for the empty string, mine will match the empty string and fail for ,. (and that his is more case-insensitive than mine. With mine you'll have to add case-insensitivity to the options of your regex function, but it's like your example)
I am not sure which regex engine/language you are using, but there is often something like a negative character groups [^a-z] meaning "everything other than a character".
I'm fighting for some time to create a QRegExp for matching the string (??) (ie: an opening parantheses, two question marks and a closing parantheses and this should be a separate word, so before and after this can be spaces, tab, newline), The closest I came up with is QRegExp("\\b\\(\\?\\?\\)\\b"); but even this is not matching it... Can you help me with this?
Thanks
f.
I don't know QRegexp, but \b only matches between alphanumeric and non-alphanumeric characters, so your regex would match (??) only if it was directly surrounded by alnums (like abc(??)123).
So you need a different approach. Hoping QRegexp supports lookaround, you could use
QRegExp("(?<=\\s|^)\\(\\?\\?\\)(?=\\s|$)");
so the regex checks if there is whitespace or start/end of string before/after (??).
If that doesn't work, you'll have to match the whitespace explicitly:
QRegExp("(?:\\s|^)\\(\\?\\?\\)(?:\\s|$)");
You could try with \B instead of \b:
QRegExp("\\B\\(\\?\\?\\)\\B");
How can I match all characters including new line with a regex.
I am trying to match all characters between brackets "()". I don't want to activate Dot matches all.
I tried
\([.\n\r]*\)
But it doesn't work.
(.*\) This doesn't work if there is an new line between the brackets.
I have been using http://regexpal.com/ to test my regular expressions. Tell me if you know something better.
I'd usually use something like \([\S\s]*\) in this situation.
The [\S\s] will match any whitespace or non-whitespace character.
The first example doesn't work because inside a character class the dot is treated literally (Matches the . character instead of all characters).
\((.|[\n\r])*\)