"space or no space" is the same as "zero or one space", or perhaps "zero or more spaces", I'm not sure exactly what you want.
In the following discussion, I'm going to use <space> to represent a single space, since a single space is hard to see in short code snippets. In the actual regular expression, you must use an actual space character.
zero-or-one-space is represented as a single space followed by a question mark (<space>?). That will match exactly zero or one spaces. If you want to match zero or any number of spaces, replace the ? with * (eg: <space>*)
If by "space" you actually mean "any whitespace character" (for example, a tab), you can use \s which most regular expression engines translate as whitespace. So, zero-or-one of any whitespace character would be \s?, and zero-or-more would be \s*
"space or no space" is the same as "zero or one space", or perhaps "zero or more spaces", I'm not sure exactly what you want.
In the following discussion, I'm going to use <space> to represent a single space, since a single space is hard to see in short code snippets. In the actual regular expression, you must use an actual space character.
zero-or-one-space is represented as a single space followed by a question mark (<space>?). That will match exactly zero or one spaces. If you want to match zero or any number of spaces, replace the ? with * (eg: <space>*)
If by "space" you actually mean "any whitespace character" (for example, a tab), you can use \s which most regular expression engines translate as whitespace. So, zero-or-one of any whitespace character would be \s?, and zero-or-more would be \s*
Other alternatives for the space or no-space problem (notice the spaces at the beginning in some):
[ ]{0,1}
{0,1}
[ ]?
?
Regex Zero or More Spaces Within a Group of Digits - Stack Overflow
regular expression - sed to match zero or more number of spaces in a string - Unix & Linux Stack Exchange
java - Regex for ONE-or-more letters/digits And ZERO-or-more spaces - Stack Overflow
allow for zero or more spaces in regex with grep - Stack Overflow
You can do:
((?:\d\s*){1,3})
Demo
Explanation:
((?:\d\s*)){1,3}
^ ^ define a non capturing group
^ a single digit
^ a space zero or more times
^ ^ capture that group (digit and following space pattern)
^ 1 to 3 times
You can also do:
^(\d\s*\d?\s*\d?\s*)
^ ^ capture group
^ one digit
^ zero or more spaces
^ optional digit
^ zero or more spaces
^ ^ etcetera.....
Demo
(\d{1,3}\s*) should get you what you want, I think. That defines a group comprised of 1-3 digits, and any space that appears before the next group of digits. If you didn't want to include the space in the group, you could use (\d{1,3})\s*
Your sedPattern has some issue with quotes. you are trying to match the same quote twice.
Also, + is used for one or more. for 0 or more, use *.
caution: code below is untested, but should get you going.
sedPattern='s/^.*"keyVal"[ ]*:.*\(".*"\).*$/\1/'
You should quote variable expansions (just one example from your code):
sed $finalSedPattern
is unquoted and will be split on spaces (when used). You should use:
sed "$finalSedPattern"
Your function with quotes:
function getVal {
sedPattern='s/^.*"keyVal":"\([^"]*\)".*$/\1/1'
finalSedPattern="${sedPattern/keyVal/$2}"
echo "
finalSedPattern" <<< "$1")"
}
And with optional spaces:
function getVal {
sedPattern='s/^.*"keyVal"[ ]\{0,\}:[ ]\{0,\}"\([^"]*\)".*$/\1/1'
finalSedPattern="${sedPattern/keyVal/$2}"
echo "
finalSedPattern" <<< "$1")"
}
I believe you can do something like this:
([ ]*+[0-9A-Za-z]++[ ]*+)+
This is 0 or more spaces, followed by at least 1 alphanum char, followed by 0 or more spaces
^^ that whole thing at least once.
Using Pshemo's idea of possessive quantifiers to speed up the regex.
The most simple answer
* means zero or more equivalent to {0,}
+ means one or more equivalent to {1,}
so look at this
[A-Z]+ means at least one Capital Letter, can be written as [A-Z]{1,}
[!@#$%&]. means you can have these Special Characters zero or more times can be written as [!@#$%&]{0,}
sorry but
the
purposeof thisanswerto beas Simple as possible
Try
grep -oP 'mem\s*=\s*\K[0-9]+' file
The \K simply drops everything matched so far, printing only the number.
This alternative to a look-behind assertion (such as (?<=mem=)) bypasses the latter's limitation that Tomalak mentions: look-behind assertions must be fixed-length.
Note: The nonstandard -P option - for PCRE (Perl-Compatible Regular Expression) support - requires GNU grep.
You can do two grep calls like so:
grep -oP 'mem *= *[0-9]+' | grep -oP '[0-9]+'
Assuming you only care about spaces in the word, not leading or trailing then you could use this:
@[A-Za-z0-9]* ?[A-Za-z0-9]*
Explanation:
@ Starts with literal @
[A-Za-z0-9] Any letter or number
* Letter or number can be length {0,infinity}
? Space char, 0 or one times
[A-Za-z0-9]* Any number of trailing letters or spaces after the space (if there is one)
You could try the below regex to match the words which starts with @ follwed by any number of letters or numbers with an optional space,
^@[a-zA-Z0-9]+ ?[a-zA-Z0-9]*$
DEMO
Java pattern would be,
"^@[a-zA-Z0-9]+ ?[a-zA-Z0-9]*$"
Your regex should work 'as-is'. Assuming that it is doing what you want it to.
wordA(\s*)wordB(?! wordc)
This means match wordA followed by 0 or more spaces followed by wordB, but do not match if followed by wordc. Note the single space between ?! and wordc which means that wordA wordB wordc will not match, but wordA wordB wordc will.
Here are some example matches and the associated replacement output:

Note that all matches are replaced no matter how many spaces. There are a couple of other points: -
(?! wordc)is a negative lookahead, so you wont match lineswordA wordB wordcwhich is assume is intended (and is why the last line is not matched). Currently you are relying on the space after?!to match the whitespace. You may want to be more precise and use(?!\swordc). If you want to match against more than one space before wordc you can use(?!\s*wordc)for 0 or more spaces or(?!\s*+wordc)for 1 or more spaces depending on what your intention is. Of course, if you do want to match lines with wordc after wordB then you shouldn't use a negative lookahead.*will match 0 or more spaces so it will match wordAwordB. You may want to consider+if you want at least one space.(\s*)- the brackets indicate a capturing group. Are you capturing the whitespace to a group for a reason? If not you could just remove the brackets, i.e. just use\s.
Update based on comment
Hello the problem is not the expression but the HTML out put that are not considered as whitespace. it's a Joomla website.
Preserving your original regex you can use:
wordA((?:\s| )*)wordB(?!(?:\s| )wordc)
The only difference is that not the regex matches whitespace OR . I replaced wordc with \swordc since that is more explicit. Note as I have already pointed out that the negative lookahead ?! will not match when wordB is followed by a single whitespace and wordc. If you want to match multiple whitespaces then see my comments above. I also preserved the capture group around the whitespace, if you don't want this then remove the brackets as already described above.
Example matches:

The reason I used a + instead of a '*' is because a plus is defined as one or more of the preceding element, where an asterisk is zero or more. In this case we want a delimiter that's a little more concrete, so "one or more" spaces.
word[Aa]\s+word[Bb]\s+word[Cc]
will match:
wordA wordB wordC
worda wordb wordc
wordA wordb wordC
The words, in this expression, will have to be specific, and also in order (a, b, then c)