Very strange problem with Ruby and regex

I get completely different results from string.scan and several regex testers ...

I'm just trying to grab a domain from a string, this is the last word.

Corresponding regex:

/([a-zA-Z0-9\-]*\.)*\w{1,4}$/

String (1 single string checked in runty runtime btw)

str = 'Show more results from software.informer.com'

Work fine, but in ruby ​​....

irb(main):050:0> str.scan /([a-zA-Z0-9\-]*\.)*\w{1,4}$/
=> [["informer."]]

I would think that I would get a match on software.informer.com, which is my goal.

+3
source share
4 answers

It does not look as if you were expecting more than one result (especially when regex is bound). In this case, there is no need to use a scan.

'Show more results from software.informer.com'[ /([a-zA-Z0-9\-]*\.)*\w{1,4}$/ ]
#=> "software.informer.com"

( , , ), (?:) , .

'foo.bar.baz lala software.informer.com'.scan( /(?:[a-zA-Z0-9\-]*\.)*\w{1,4}/ )
#=> ["foo.bar.baz", "lala", "software.informer.com"]
+2

, , String # scan. :

" , , ."

, , , .

+3

software.informer.com. $&. scan - . , .com scan.

Ruby ( ). , , . scan irb, , , scan ( ), , . , , .

+2

:

/([a-zA-Z0-9\-]*\.*\w{1,4})$/

informer.com

.

http://rubular.com/regexes/13670

0

Source: https://habr.com/ru/post/1732883/


All Articles