regex - Perl Regular Expression extracting sub-string? -


i have string variable containing abcd.asd.qwe.com:/dir1. want extract abcd portion i.e. portion beginning till first appearance of .. problem there can characters (only alphanumeric) of length before .. created regexp.

if($arg =~ /(.*?\.?)/) {     $temp_name = $1; } 

however giving me blank string. logic :

.*? - character non-greedily \.? - till first or none appearance of . 

what wrong?

you can instead use negative character class this

^[^.]+ 

[^.] match character except .

[^.]+ match 1 many characters(except .)

^ depicts start of string

or

^.+?(?=\.|$) 

(?=) lookahead checks particular pattern after current position..so text abcdad regex a(?=b) a match

$ depicts end of line(if used multiline option) or end of string(if used singleline option)


Comments