This question already has answers here:
Check if string contains single backslashes with regex
(3 answers)
Closed 3 years ago.
I have trouble with figuring out this regex:
https://regex101.com/r/WtAYVa/2
It works capturing the first single backslash (\), but I want to ignore (\\), especially, when there's a space after \\.
If we wish to fail the double backslash, and only pass the single one, we would be simply adding more boundaries to our expression, such as we would be using start and end anchors:
^\\\s$
Demo
Related
This question already has answers here:
Regular expression to stop at first match
(9 answers)
How do I match everything after # until space?
(4 answers)
Regex everything after x until space
(1 answer)
Closed 28 days ago.
I really tried hard looking over the internet for an hour or so, trying to find if this question has already an answer somewhere, but no joy. If it already has an answer somewhere, feel free to link and close this one.
I am trying to match from AFTER a specific character until the FIRST space.
This is an example of the source string
blabla/1.2.3 [other stuff I dont care about]
I just want 1.2.3
I have tried so many different variants which I am not gonna pollute here all of them.
But the one I am most intrigued about is
\/.*\s
Apart from matching the / which I want to exclude, why does this match until the end of the line and not until the first space?
Other things I have tried
\/\b This just matches /
\/.*\b Matches almost everything until ]
\/.*\s? Again until end of line
\/.*(\s)? Ditto
\/.*\ Matches until the LAST whitespace excluding newline
And so on...
This question already has answers here:
Why is a character class faster than alternation?
(2 answers)
Using alternation or character class for single character matching?
(3 answers)
Closed 3 years ago.
Why is:
[\s\S]+?
Much more efficient than:
(?:.|\n)+?
What are the differences between the two in terms of how they work behind the scenes?
Note: this is with DOTALL turned off. Also, from https://www.regular-expressions.info/dot.html:
JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [\s\S] to match any character. This character matches a character that is either a whitespace character (including line break characters), or a character that is not a whitespace character. Since all characters are either whitespace or non-whitespace, this character class matches any character.
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
How to extract a substring using regex
(14 answers)
Closed 3 years ago.
I'm trying to find the word: <*hasburnt*> in the string below using the this regex: <\*.*(bur).*\*>
But it gives me both <*hasburnt*> <*electrical*>. How do I just get <*hasburnt*> ?
bench testedstarter, starter just makes noise, and <*hasburnt*>
<*electrical*> smell.
Try this: /<.*?(bur).*?>/
Regex101 demo
The reason for ? here is because .* tries to match as much characters as possible, so it also matches <electrical. .*? makes it lazy - trying to match as little as possible, and as such ending the match at <hasburnt>.
EDIT: using ? for the first .* would make <hasburnt> independent of positions of similar strings.
This question already has answers here:
RegEx for allowing alphanumeric at the starting and hyphen thereafter
(4 answers)
Closed 5 years ago.
I want to build a regular expression which only matches [A-Za-z0-9\-] with an additional rule that hyphens (-) are not allowed to appear at the start and at the end.
For example:
my-site is matched.
m is matched.
mysite- is not matched.
-mysite is not matched.
Currently, I've come up with ^[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]+$.
But this doesn't match m.
How can I change my regular expression so that it fits my needs?
Use look arounds:
^(?!-)[A-Za-z0-9-]*(?<!-)$
The reason this works is that look arounds don't consume input, so the look ahead and the look behind can both assert on the same character.
Note that you don't need to escape the dash within the character class if it's the first or last character.
This question already has answers here:
Get final special character with a regular expression
(2 answers)
Closed 8 years ago.
Tried looking at the regex that some others are using, but for some reason it's not working for me.
I just basically have a string, such as "testing-user#example.com", It'll only extract user#example.com and not the whole thing.
Here's what I have:
regex = Regexp.new(/\b[a-zA-Z0-9._%+-,]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/)
email = line.scan(regex)
Any help would be greatly appreciated.
The hyphen needs to be escaped for the position it is at inside of the character class.
[a-zA-Z0-9._%+\-,]+
^
(+-,) currently matches a single character in the range between + and ,
Inside of a character class the hyphen has special meaning. You can place the hyphen as the first or last character of the class. In some regex implementations, you can also place directly after a range. If you place the hyphen anywhere else you need to precede it with a backslash it in order to add it to your class.