This question already has answers here:
Regex that matches anything except for all whitespace
(5 answers)
Closed 2 years ago.
I'm using a regular expression to match Facebook url.
((http|https)://)?(www[.])?facebook.com/.+
It accepts:
facebook.com/xxxxx
www.facebook.com/xxxxx
http://facebook.com/xxxxx
https://facebook.com/xxxxx
But it still accepts whitespaces after /:
facebook.com/(spaces there)
How can I prevent it?
You can shorten the pattern by making the s optional in https using a quesion mark, and use \S+ to match 1 or more non whitespace characters instead of .+ which can also match spaces.
(?:https?://)?(?:www\.)?facebook\.com/\S+
Regex demo
Related
This question already has answers here:
What is a word boundary in regex?
(13 answers)
Difference between \b and \B in regex
(10 answers)
Regex using word boundary but word ends with a . (period)
(4 answers)
Closed 3 months ago.
I am trying to match an exact word with multiple patterns using regex.
What I tried:
pattern: "\b(#DOG|#DOG1|#DOG2)\b", text: "#DOG2".
I want to match exact text "#DOG2" using these patterns, and the expected result should be: there is only 1 match "#DOG2". How can I change the pattern text to achieve this?
Interestingly, if I remove all '#' symbol from above text, the regex will work as expected:
pattern: "\b(DOG|DOG1|DOG2)\b", text: "DOG2".
Does '#' affect the result? How can I avoid this?
This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 2 years ago.
Hey I have a list of files
B123245.xml
B123245-ext.xml
1234W01.xml
1234W01-ext.xml
Now I need a regular expression filter only the files without -ext in the name.
I tried already this ^.+(?!-ext)\.xml$
but it is not working.
What am I doing wrong?
Not sure about your exact needs, but if you want to exclude those file where "-ext" is right before the xml extension I think you could use:
^.+(?<!-ext)\.xml$
See the demo
^ - Start string anchor.
.+ - 1+ character apart from newline.
(?<!-ext) - A negative lookbehind to assert position isn't preceded by "-ext".
\.xml - Match a literal dot and "xml".
$ - End string anchor.
With the help of user 'The fourth bird' I found out the correct structure.
Here is the correct result
^(?!.*-ext).+\.xml$
This question already has answers here:
What is a non-capturing group in regular expressions?
(18 answers)
Closed 2 years ago.
I have this regexp:
^(?<FOOTER_TYPE>[ a-zA-Z0-9-]+)?(?<SEPARATOR>:)?(?<FOOTER>(?<=:)(.|[\r\n](?![\r\n]))*)?
Which I'm using to match text like:
BREAKING CHANGE: test
my multiline
string.
This is not matched
You can see the result here https://regex101.com/r/gGroPK/1
However, why is there the last Group 4 ?
You will need to make last group non-capturing:
^(?<FOOTER_TYPE>[ a-zA-Z0-9-]+)?(?<SEPARATOR>:)?(?<FOOTER>(?<=:)(?:.|[\r\n](?![\r\n]))*)?
Make note of:
(?:.|[\r\n](?![\r\n]))*)?
(?: at the start makes this optional group non-capturing.
Updated Demo
it is group 4 because the fourth parentheses you defined is:
(.|[\r\n](?![\r\n]))*)
it translate to
"either dot, or the following regex"
and in the example you have, it ends on a dot.
string.
so as regex is usually greedy, it captures dot as the forth group
This question already has answers here:
Have trouble understanding capturing groups and back references
(2 answers)
Closed 3 years ago.
The url of my username is:
https://stackoverflow.com/users/12283851/user12283851
For this username it looks like the regular expression might be close to:
r'https?://stackoverflow.com/users/\d{1,9}/user\d{1,9}'
Is there a way in the regex to make sure that the first ID matches the second? In other words:
https://stackoverflow.com/users/12283851/user12283851 <== Valid
https://stackoverflow.com/users/11111111/user12283851 <== Invalid
This is accomplished by using backreferences.
The backreference \1 (backslash one) references the first capturing group. \1 matches the exact same text that was matched by the first capturing group
In your example the following regex would work:
https?://stackoverflow\.com/users/(\d{1,9})/user\1
See this demo
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