This question already has answers here:
Reference - What does this regex mean?
(1 answer)
\B+ vs [\B]+ vs [^\b]+ in Python regex
(2 answers)
What's the use of the [\b] backspace regex?
(3 answers)
Closed 5 years ago.
Is there a difference between [^\b] and .?
I was modifying some code created by someone else that included this no-word-boundary-character-class ([^\b]). and am not able to find a difference between that and wildcard . (this is in ruby).
My assumption was that [^\b]+ when applied to the string hello world should match hello and stop before the space, (as that is where there is a word boundary.
My observation is that it seems to just match everything. rubular link.
What should be happening here?
[\b] means backspace and [^\b] not a backspace
\b is not a character, it can't be included in a character class.
The negation of a word boundary is \B
Related
This question already has answers here:
Emacs - regular expressions in Lisp need to be double-escaped - why?
(4 answers)
Closed 4 years ago.
I want to search for an regular expression with the function re-search-forward
When I tried using the examples from the page
here: https://www.emacswiki.org/emacs/RegularExpression#toc1
specifically the regular expression \w\{20,\} used to search for a word with 20 letters or more, I get an error.
Here I am placing my cursor after the closing parenthesis in my Lisp buffer and pressing C-x C-e for evaluating it.
However, when I use the Regexp I-search via,
C-M-s it highlights the correct word as expected.
Why is this?
This regexp:
\w\{20,\}
is expressed in a double-quoted elisp string like so:
"\\w\\{20,\\}"
Backslashes are special to the double-quoted read syntax for strings as well as being special to regexp syntax; so if a backslash is for the regexp, you need to double it.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I was reading about the match method in ruby, I understood most of the example given at Regexp
But I am failing to understand, why is:
/[0-9a-f]/.match('9f')
=> #<MatchData "9">
And not:
=> #<MatchData "9f">
I might be missing some basic understanding of Regex, so bear with me.
Because you're asking it to match a single character of class 0-9 or a-f.
If you want to match multiple use a plus or an asterisk after the character classes e.g. /[0-9a-f]+/.match('9f')
It's all here.
This question already has answers here:
How to escape all characters with special meaning in Regex
(2 answers)
Closed 7 years ago.
I know this might be asked time and again. But I'm really stuck with this. I've got it to work for including numbers and alphabets but I have no idea on how to include "/" also.
This is what I have,
name.gsub!(/[^0-9A-Za-z]/, '')
So if name is "Cool Stuff *(#/" it returns "CoolStuff". I'd just like it to return "CoolStuff/".
The / is a special character that must be 'escaped' (meaning to take the / literally, and not for a switch or special meaning). So you have:
name.gsub!(/[^0-9A-Za-z]/, '')
But also realize you could shorten your RegEx statement by making it case insensitive by adding an 'i' after the ending slash and therefore allowing you to drop either the [A-Z] or the [a-z] part. So you could have instead:
name.gsub!(/[^0-9A-Z\/]/i, '')
Hope this helps!
This question already has answers here:
How to extract URLs from text
(6 answers)
Closed 8 years ago.
I am tring to extract a link from a phrase and it could be any where last, first or middle so I am usig this regex
link=text.scan(/(^| )(http.*)($| )/)
but the problem is when the link is in the middle it gets the whole phrase until the end.
What should I do ?
It's because .* next to http is greedy. I suggest you to use lookarounds.
link=text.scan(/(?<!\S)(http\S+)(?!\S)/)
OR
link=text.scan(/(?<!\S)(http\S+)/)
Example:
> "http://bar.com foo http://bar.com bar http://bar.com".scan(/(?<!\S)http\S+(?!\S)/)
=> ["http://bar.com", "http://bar.com", "http://bar.com"]
DEMO
(?<!\S) Negative lookbehind which asserts that the match won't be preceeded by a non-space character.
http\S+ Matches the substring http plus the following one or more non-space characters.
Do all the links you are trying to match follow some simple pattern? We'd need to see more context to confidently provide a good solution to your problem.
For example, the regex:
link=text.scan(/http.*\.com/)
...might be good enough for the job (this assumes all links end in ".com"), but I can't say for sure without more information.
Or again, for example, perhaps you could use something like:
link=text.scan(/http[a-z./:]*) - this assumes all links contain only lower case letters, ".", "/" and ":".
This question already has answers here:
Explanation of Lookaheads in This Regular Expression
(5 answers)
Closed 9 years ago.
I am new to regex, and I am trying to break down the regex so I can understand it better:
/(\d{3})(?=\d)/
I understand that (\d{3}) is capturing 3 digits, but unsure what the second portion is trying to capture.
What does ?= mean?
(?=\d) is a positive lookahead it means match & capture 3 digits that are followed by a digit.
So something like this will happen:
1234 => capture 123
123a => no match
(?=pat) - Positive lookahead assertion: ensures that the following characters match pat, but doesn't include those characters in the matched text
/(\d{3})(?=\d)/ - Here (\d{3}) is capturing 3 digits, followed by a digit,but last digit not to be captured in that group.
Look here, here and here
Hope this will help!