This question already has answers here:
regular expression for anything but an empty string
(9 answers)
Closed 4 years ago.
I have the following code
https://stackblitz.com/edit/angular-uvxifq-qrjtpg
pattern="^\s+$"
I need to negate this, because as of now I got error when I put a letter, but it should only show error IF there is only white space.
I tried using ?! but it shows an error
You can use the following regex:
^(?: *[^\s] *)+$
demo: https://regex101.com/r/th0A3A/3/
If you want to use a negative lookahead (?!, you could check if from the start till the end of the string there are no whitespace characters:
pattern="^(?!\s*$)[\s\S]+$"
Regex demo
That will match
^ Start of string
(?!\s*$) Negative lookahead, assert that what follows is not 0+ whitespace characters and end of string
[\s\S]+ Match 1+ times any character including new lines
$ End of string
Related
This question already has answers here:
Regex how to match an optional character
(5 answers)
Closed 9 months ago.
I want to get all Strings that start with a "$" sign followed by an integer or have exactly two digits after the decimal point.
e.g. $7.26 and $48.49 and $17
but not $.49 and $192.9
That's my regular expression so far: ^[$]\d+**[.][0-9][0-9]**
I want the marked part to be optional or the string has to end.
Also, how could I use [0-9]{2} instead of [0-9][0-9]?
Try this pattern: ^\$\d+(?:\.\d{2})?$
See Regex Demo
Explanation
^: Start of the string.
\$: Match with the character $.
\d+: Match with one or more digits between 0-9.
(?:: Start of the non-captured group.
\.: Match with the dot character.
\d{2}: Match exactly two digits.
): End of the group.
?: Make everything in the group optional.
$: End of the string.
Note: the $ and . character in regex means respectively end of the string and everything, so if we want to capture exactly the $ character (not the end of the string) we should escape those characters.
If I understand you correctly, you need to escape characters, which are used as part of syntax of RegEx. Try used backslash
^$\d+(?:.[0-9][0-9])?$
Or used example in first answer - with {2} - it's more correct
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
How to negate specific word in regex? [duplicate]
(12 answers)
Closed 3 years ago.
I want to validate a string with alpha numeric values, but if the string contains -- (double dash) anywhere in the string, it should be invalid.
valid:
apple123
-apple-123
app-le123
a-p-p-l-e-1-2-3
invalid:
--apple123
app--le123
https://stackoverflow.com/a/1240365/1920590
The above old post have the answer ^(?!.*bar).*$ which does the negation, but it does not work for same character repetition like --.
Can anyone help me to figure out to modify the ^(?!.*bar).*$ to identify -- as a string.
You may use a negative lookahead:
^(?!.*--)[\w-]+$
(?!.*--) is a negative lookahead assertion that will fail the match if -- appears anywhere in input.
[\w-] matches a word character [a-zA-Z0-9_] or a hyphen
RegEx Demo
This question already has answers here:
In regex, match either the end of the string or a specific character
(2 answers)
Closed 3 years ago.
I have a string and I need to match a pattern and discard the rest of the string after the first occurrence of a delimiter after the patter matched
sample string :
_fw_xx_app_id=xxxxx&adobe_id=59742375247590810332565440942222920249&krux_user=yyyyyyy&User_Agent=zzzzzz&_fw_did_idfa=aaaaaaa&_fw_dpr=2.00&_fw_ae=nonauthenticated&_fwu%3A386123%3Atqxqle5of=1&_fwu%3A386123%3Atshc3wdb8=1
I want to extract only the value that is after key '_fw_ae=' until the first occurrence of &
I tried this
regexp_substr(all_request_kv,'_fw_ae=(.+?)&|$',1,1,'e')
but this is bringing everything after the _fw_ae=
like below
_fw_ae=nonauthenticated&_fwu%3A386123%3Atqxqle5of=1&_fwu%3A386123%3Atshc3wdb8=1
where as i only want nonauthenticated in this example
Instead of a non greedy quantifier, you could use a negated character class [^&] matching not an & inside a capturing group:
_fw_ae=([^&]+)
Regex demo
This question already has answers here:
RegExp exclusion, looking for a word not followed by another
(3 answers)
Closed 3 years ago.
I'm trying to match the string "this" followed by anything (any number of characters) except "notthis".
Regex: ^this.*(?!notthis)$
Matches: thisnotthis
Why?
Even its explanation in a regex calculator seems to say it should work. The explanation section says
Negative Lookahead (?!notthis)
Assert that the Regex below does not match
notthis matches the characters notthis literally (case sensitive)
The negative lookahead has no impact in ^this.*(?!notthis)$ because the .* will first match until the end of the string where notthis is not present any more at the end.
I think you meant ^this(?!notthis).*$ where you match this from the start of the string and then check what is directly on the right can not be notthis
If that is the case, then match any character except a newline until the end of the string.
^this(?!notthis).*$
Details of the pattern
^ Assert start of the string
this Match this literally
(?!notthis)Assert what is directly on the right is notnotthis`
.* Match 0+ times any char except a newline
$ Assert end of the string
Regex demo
If notthis can not be present in the string instead of directly after this you could add .* to the negative lookahead:
^this(?!.*notthis).*$
^^
Regex demo
See it in a regulex visual
Because of the order of your rules. Before your expression would get to negative lookahead, prior rules has been fulfilled, there is nothing left to match.
If you wish to match everything after this, except for notthis, this RegEx might also help you to do so:
^this([\s\S]*?)(notthis|())$
which creates an empty group () for nothing, with an OR to ignore notthis:
^this([\s\S]*?)(notthis|())$
You might remove (), ^ and $, and it may still work:
this([\s\S]*?)(notthis|)
This question already has answers here:
Regex match entire words only
(7 answers)
Closed 6 years ago.
i want to match a very simple number-bar-number pattern : 1/2
My regex is: ([0-9]{1}\/[0-9]{1})
The problem is that I match things I want to exclude. I need exact matching excluding the rest.
My regex return as valid patterns as :
1/12344
2/23ABC
2/233423/2425
[update]
tested with some txt files using GREP, still having issues. By instance:
2/3/16 (it's a date and it matches the pattern, so grep returned the entire line)
I'm not very versed on regex so any help would be very much appreciated
Regards
Try this
(?:^|\s)(\d+\/\d+)(?=\s|$)
Regex demo
Explanation:
(?: … ): Non-capturing group sample
^: Start of string or start of line depending on multiline mode sample
|: Alternation / OR operand sample
\: Escapes a special character sample
( … ): Capturing group sample
+: One or more sample
(?=…): Positive lookahead sample
$: End of string or end of line depending on multiline mode sample