Regex validation: Allow new lines but not spaces [duplicate] - regex

This question already has answers here:
How to matches anything except space and new line?
(3 answers)
Closed 3 years ago.
I know there are loads of questions on this site around regex and validating against white spaces, believe me I've spent the past few hours looking. I've been unable to create a regex validation that matches the following requirements:
Fail validation if there are any spaces in this text (including at the start and end of the text)
Allow validation if the text has new lines in it
I very quickly found /s was not a good option as this fails on my second point. The closest I've managed to get is [A-Z]*[a-z]*[\" *"$] which flags exactly the reverse (spaces pass but everything else fails).
I've tried reversing it somehow but not having much success.

Anchor to the beginning of the string, repeatedly match anything but a space with [^ ]*, and anchor to the end of the string:
^[^ ]*$
This matches 0 or more non-space characters, but will permit newlines. (If you want 1 or more non-space characters, use + instead of *)

Related

Match character and any whitespaces around it [duplicate]

This question already has answers here:
Splitting a String by number of delimiters
(2 answers)
Closed 2 years ago.
I have a file containing informations in the following format :
Fred,Frank , Marcel Godwin , Marion,Ryan
I need the match commas and any whitespace around them, but not any comma inside brackets.
My problem is that with my current regex [\s,]+ the whitespaces between words are matched. So in this example the whitespace between Marcel and Godwin.
I thought about using something like \s,\s* but it wouldn't match parts when there is no whitespace around the comma, like between Fred and Frank
Surely, it's a simple fix but I can't figure it out.
I think this will match the commas including the whitespace before and afterwards like you explained in your question.
\s*(?=\,)\,(?<=\,)\s*
This is a positive looahead: (?=\,), it means it matches any whitespace if there is a comma afterwards.
This is a positive lookbehind: (?<=\,), it means it matches any whitespace if there is a comma rigth before.
Try it out yourself. You can use this page to check the output in your browser.

regex allow white space, but do not begin or end with one, and not all white spaces [duplicate]

This question already has answers here:
RegEx for no whitespace at the beginning and end
(18 answers)
Closed 2 years ago.
I am trying to implement a regex rule that would allow users to enter white spaces, but it should not be all white spaces, nor begin or end with a white space. My research did not yield any workable solution. Currently, my rule looks like this, and I would like to add the white space requirement on top of that:
let string_rule = "^[a-zA-Z0-9_-]{3,16}$";
let string_rule = "^\S[a-zA-Z0-9_-]{3,16}\S$";
you can use following regex:
^(?:\S.*?\S|\S)$
Regex Demo
Details
^: start of string
\S.*?\S: first state which your input have more than 1 character, then its first and last character must exist and not to be spaces
\S: second state when your input consists of only one character
$: end of string

Extract String Between Slashes, But Second Slash May Not Exists [duplicate]

This question already has answers here:
In regex, match either the end of the string or a specific character
(2 answers)
Closed 2 years ago.
I'm trying to figure out how to extract usernames from a URL that's captured in a form. I do have the below regex, but the issue is that the second forward slash may not exist. Here are the examples:
Sample URLs
https://test.site.com/u/username
https://test.site.com/u/username/pref/summary
I'm trying to extract the username.
Current Regex
/u/(.*?)/
The current one I have above successfully extracts the username, but only when there is another / after the username. The second / needs to be optional; it may or may not be there, and there may or may not be more after that.
I just couldn't find the correct regex to make the second / optional (using ? at the end didn't help) but not exactly "optional," if that makes sense.
Thanks in advance!
/u/([^/]*) will match as many non-/ characters after /u/ as possible.
It will not match pref and summary,
because [^/] matches any character other than /,
so [^/]* matches a string (as long as possible)
of characters other than /. 
Consider: if your pattern is B[aeiou]*
and your input is Beetles (or Beethoven),
it will match only Bee,
stopping at (before) the first character that isn’t a vowel. 
Similarly, [^/]* stops at (before) the first occurrence of /.

Regular Expression - Need Help Matching Everything Except For A Certain String [duplicate]

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 4 years ago.
After countless hours of trying to get this regex to work (including looking all over StackOverflow), I thought I'd reach out for help on here as I have not been successful).
I have tried creating a regex to match everything and to not match any parameters that look like this:
text=3242ffs3F34
The data after the = sign can be random (its a mixture of numeric and string characters) and is never the same. So far I have created the following regex below, which is almost doing what I am after but it does not work.
\b(?!text=.*)\b\S+
Assistance is much appreciated!
EDIT:
I will be using the regex to match everything in a file but to filter out all parameters that look like this:
text=3242ffs3F34
Below is an example of how the config file will look like:
This is a test
test=asda
test2=22rr2
text=3242ffs3F34
test5=hello
To match everything except strings containing LAST_DOMINO_TIME= as substring you can use the expression:
(?!.*\bLAST_DOMINO_TIME=.*$)^.*$
(?! Negative lookahead.
.* Match anything.
\b Word boundary.
LAST_DOMINO_TIME= Literal substring.
.*$ Anything up to end of string.
) Close lookahead.
^.*$ Assert position beginning of line, match anything up to end of line.
You can try it here.

Regex for string containing one string, but not another [duplicate]

This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Closed 3 years ago.
Have regex in our project that matches any url that contains the string
"/pdf/":
(.+)/pdf/.+
Need to modify it so that it won't match urls that also contain "help"
Example:
Shouldn't match: "/dealer/help/us/en/pdf/simple.pdf"
Should match: "/dealer/us/en/pdf/simple.pdf"
If lookarounds are supported, this is very easy to achieve:
(?=.*/pdf/)(?!.*help)(.+)
See a demo on regex101.com.
(?:^|\s)((?:[^h ]|h(?!elp))+\/pdf\/\S*)(?:$|\s)
First thing is match either a space or the start of a line
(?:^|\s)
Then we match anything that is not a or h OR any h that does not have elp behind it, one or more times +, until we find a /pdf/, then match non-space characters \S any number of times *.
((?:[^h ]|h(?!elp))+\/pdf\/\S*)
If we want to detect help after the /pdf/, we can duplicate matching from the start.
((?:[^h ]|h(?!elp))+\/pdf\/(?:[^h ]|h(?!elp))+)
Finally, we match a or end line/string ($)
(?:$|\s)
The full match will include leading/trailing spaces, and should be stripped. If you use capture group 1, you don't need to strip the ends.
Example on regex101