Question:
Need help with regular expression?
JChance
2006-10-06 06:51:30 UTC
I'm trying to write a regular expression that will match "tester" for the following strings - the idea of this regex is to get the SLD from a user passed in domain name string:

tester
tester.com
tester.eu
tester.
www.tester.com
www.tester.eu
www.tester.
.tester.com
.tester.eu
.tester.

I've already spent about an hour, and can't get it to work perfect but I'm not regular expression expert either. Anyone else want to give it a try?
Three answers:
Prancing Stallion
2006-10-06 07:24:16 UTC
i'll assume the regex is going to be used in tools like lex or flex.(even otherwise u won't have trouble changing the regex)



i'll also assume that the input symbols include just letters and the dot(.) as per ur examples



RegEx: ( "tester" ) | ( ([a-zA-Z]+ "." )? ("tester") ("." [a-zA-Z]+) )



Hope u are comfortable with the symbols..

+ is for one or more occurences of the pattern

* is for zero or more occurences of the pattern

? is for zero or one

[a-z] range of letters (lower-case)

[A-Z] likewise for uppercase..

| is for one of the two patterns (on either side)
APHawkes
2006-10-06 10:21:22 UTC
The previous answer was close, but assumed only alphabetical values. It ignored special characters and numbers.



I'm going to assume that when you put that you're indicating any number and arrangement of any characters will be acceptable, that is, not taking as a literal value.



If that's the case, then you really only have these test cases:

tester

tester.

.tester.



The rub is that the part is most easily represented by ".*" (without quotes). The problem is that you need to have the period as a literal in your expression as well. The way you get around this is you use the escape character "\".

So, we can do this by induction



tester => tester

tester. => tester\..*

.tester. => .*\.tester\..*



Put it all together and you've got:



.*\.tester\..*|tester\..*|tester



I would probably do the most complicated one first, just so that it will match on the entire string "abc.tester.com" instead of just the "tester" part.
turick
2016-11-27 01:54:12 UTC
it truly is what it skill: / it really is the start of the reg expr ^ shows the tournament could start up on the start of a line of textual content (11+) the parens are used to crew words. accordingly, it really is going to tournament a "a million", followed by skill of one or better "a million"s. a million+ the "" is an destroy out char, turning off the particular which technique of reg expr trend chars. accordingly, it really is superfluous. It only says tournament a "a million". this will be an mistakes. The + says tournament one or better of those. $ tournament on the top of the textual content line So, all this component is doing is only attempting to compare strains which have a minimum of three "a million"s like this: "111" it might want to have only been written as /^111+$/. hence, both you probably did not replica it properly, or the author made an mistakes.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...