Regular expression email issue - regex

In my email regex i want following output
abc#abc.co.in
I am writing down below
^[\w]+#[a-z]+([.a-z]+)
Issue is coming in .co.in
I want to iterate the ".co" part not more then 2 times and it should be more then 1 and less then or equals to 2
I tried below but not working
^[\w]+#[a-z]+([.a-z]{1,2})

You need to take the . out of your character class and escape it:
^\w+#[a-z]+(\.[a-z]+){1,2}$
Also changed your [\w] to \w and added a $ to the end so that the whole string must match, not just the beginning.

I think you want this:
^\w+#[a-z]+(\.[a-z]+){1,2}
Note that your [\w] element is unnecessary, \w is sufficient.

your dot in this case means "any character". it must be escaped. try this:
^[a-z]+[a-z0-9_]*#[a-z]+[a-z0-9\-]*\.[a-z]{1,2}\.[a-z]{1,2}$

You can use it like this. \w+#\w+(\.\w{2}){2}

Related

regex limiting wildcards for url folders

I'd like to set up a regular expression that matches certain patterns for a URL:
http://www.domain.com/folder1/folder2/anything/anything/index.html
This matches, and gets the job done:
/^http:\/\/www\.domain\.com\/folder1\/folder2\/.*\/.*\/index\.html([\?#].*)?$/.test(location.href)
I'm unsure how to limit the wildcards to one folder each. So how can I prevent the following from matching:
http://www.domain.com/folder1/folder2/folder3/folder4/folder5/index.html
(note: folder 5+ is what I want to prevent)
Thanks!
Try this regular expression:
/^http:\/\/www\.domain\.com\/(?:\w+\/){1,3}index\.html([\?#].*)?$/
Change the number 3 to the maximum depth of folders possible.
. matches any character.
[^/] matches any characters except /.
Since the / character marks the begining and end of regex literals, you may have to escape them like this: [^\/].
So, replacing .* by [^\/]* will do what you want:
/^http:\/\/www\.domain\.com\/folder1\/folder2\/[^\/]*\/[^\/]*\/index\.html([\?#].*)?$/.test(location.href)
/^http:\/\/www\.domain\.com\/folder1\/folder2\/[^/]*\/[^/]*\/index\.html([\?#].*)?$/
I don't remember whether we should escape the slashes within the []. I don't think so.
EDIT: Aknoledging tom's comment using + instead of *:
/^http:\/\/www\.domain\.com\/folder1\/folder2\/[^/]+\/[^/]+\/index\.html([\?#].*)?$/
/^http:\/\/www\.domain\.com\/\([^/]*\/\)\{2\}/
And you can change 2 to whatever number of directories you want to match.
You may use:
^http:\/\/www\.domain\.com\/folder1\/folder2\/(\w*\/){2}index\.html([\?#].*)?$/.test(location.href)

Very simple regex

I need a regex to match something this:
<a space><any character/s>#<any character/s><a space>
Yes, it's a very very basic email parser.
Thanks!
Something like this? /^ [^#]+#[^ ]+ $/
The square brackets indicate a character class, which is the characters that can be present there. So, your regex would match .#. or *#*. Instead, try "\ .*#.*\ " (quotes to show the space at the end, don't include them inside your regex.
For testing e-mail, you might use the regex described here:
\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b
It still doesn't cover 100% of e-mails, but the comprehensive version is fairly involved.
^ .+#.+ $
This translates to "the start of the string is followed by a space, one or more characters, the # symbol, one or more characters, and the last character in the string is a space."

Regex or on multiple/single characters

I'm dynamically making a regex.
I want it to match the following:
lem
le,,m
levm
lecm
Basically, "lem" but before the m it can have any number of , or any one of any character. Right now I have
le[\,]{0,}[.]?m
you can see it at
http://regexr.com?303ne
It should match every one but the third one.
Update: I figured it out:
le[\,]{0,}.?m
Whenever you think "or" in Regular Expressions, you should start with alternation:
a|b
matches either a or b. So
any number of a list of characters OR 1 of any character
can be translated quite literally to
[...]*|.
where ... would be the list of characters to match (a character class). If you use that as part of a longer expression, you need to use parentheses, because concatenation binds stronger (has higher precedence) than alternation:
le([,]*|.)m
Because the character class has only one item, we can simplify this:
le(,*|.)m
Note that . by default means "any character but newline".
What about this:
le(,*|.?)m
it should do what you want.
How about this one:
([^,])(?=\\1)
But this does the opposite :-) Not sure if it is ok for you
UPD:
this should work for you:
~^(?:,|([^,])(?!\\1))+$~
not sure what dialect you're looking for, but it works in PCRE: http://ideone.com/6Q3Wk
UPD2:
the same regex included into another
$r = '(?:,|([^,])(?!\\1))+';
var_dump(preg_match('~le' . $r . 'm~', 'leem'));
In this case the final expression becomes: le(?:,|([^,])(?!\\1))+m where le and m are added around mine without modifications

Little vim regex

I have a bunch of strings that look like this: '../DisplayPhotod6f6.jpg?t=before&tn=1&id=130', and I'd like to take out everything after the question mark, to look like '../DisplayPhotod6f6.jpg'.
s/\(.\.\.\/DisplayPhoto.\{4,}\.jpg\)*'/\1'/g
This regex is capturing some but not all occurences, can you see why?
\.\{4,} is trying to match 4 or more . characters. What it looks like you wanted is "match 4 or more of any character" (.\{4,}) but "match 4 or more non-. characters" ([^.]\{4,}) might be more accurate. You'll also need to change the lone * at the end of the pattern to .* since the * is currently applying to the entire \(\) group.
I think the easyest way to go for this is:
s/?.*$/'/g
This says: delete everything after the question mark and replace it with a single quote.
I would use macros, sometime simpler than regexp (and interactive) :
qa
/DisplayPhoto<Enter>
f?dt'
n
q
And then some #a, or 20000#a to go though all lines.
The following regexp: /(\.\./DisplayPhoto.*\.jpg)/gi
tested against following examples:
../DisplayPhotocef3.jpg?t=before&tn=1&id=54
../DisplayPhotod6f6.jpg?t=before&tn=1&id=130
will result:
../DisplayPhotocef3.jpg
../DisplayPhotod6f6.jpg
%s/\('\.\.\/DisplayPhoto\w\{4,}\.jpg\).*'/\1'/g
Some notes:
% will cause the swap to work on all lines.
\w instead of '.', in case there are some malformed file names.
Replace '.' at the start of your matching regex with ' which is exactly what it should be matching.

How to match a string that does not end in a certain substring?

how can I write regular expression that dose not contain some string at the end.
in my project,all classes that their names dont end with some string such as "controller" and "map" should inherit from a base class. how can I do this using regular expression ?
but using both
public*.class[a-zA-Z]*(?<!controller|map)$
public*.class*.(?<!controller)$
there isnt any match case!!!
Do a search for all filenames matching this:
(?<!controller|map|anythingelse)$
(Remove the |anythingelse if no other keywords, or append other keywords similarly.)
If you can't use negative lookbehinds (the (?<!..) bit), do a search for filenames that do not match this:
(?:controller|map)$
And if that still doesn't work (might not in some IDEs), remove the ?: part and it probably will - that just makes it a non-capturing group, but the difference here is fairly insignificant.
If you're using something where the full string must match, then you can just prefix either of the above with ^.* to do that.
Update:
In response to this:
but using both
public*.class[a-zA-Z]*(?<!controller|map)$
public*.class*.(?<!controller)$
there isnt any match case!!!
Not quite sure what you're attempting with the public/class stuff there, so try this:
public.*class.*(?<!controller|map)$`
The . is a regex char that means "anything except newline", and the * means zero or more times.
If this isn't what you're after, edit the question with more details.
Depending on your regex implementation, you might be able to use a lookbehind for this task. This would look like
(?<!SomeText)$
This matches any lines NOT having "SomeText" at their end. If you cannot use that, the expression
^(?!.*SomeText$).*$
matches any non-empty lines not ending with "SomeText" as well.
You could write a regex that contains two groups, one consists of one or more characters before controller or map, the other contains controller or map and is optional.
^(.+)(controller|map)?$
With that you may match your string and if there is a group() method in the regex API you use, if group(2) is empty, the string does not contain controller or map.
Check if the name does not match [a-zA-Z]*controller or [a-zA-Z]*map.
finally I did it in this way
public.*class.*[^(controller|map|spec)]$
it worked