Regular Expression is working for php not for JavaScript - regex

This is my expression:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[[:graph:]]{6,25}$
I have tested it on http://regex101.com/
Its working for string DeepakManwal1 when there is php selected but does not work when javascript is selected. I don't know what is the exact reason.
I want to use this expression for password validation where there should be at-least one uppercase letter and one numeric character.
Here is fiddle http://jsfiddle.net/j7rmj44h/

It is because of the POSIX class [:graph:] — you could change it to the equivalent [\x21-\x7E]. Also, you need to remove the quotations around your pattern according to your fiddle.
var re = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[\x21-\x7E]{6,25}$/
Fiddle

AFAIK, javascript doesn't understand POSIX.
Have a try with:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])\S{6,25}$

Different regex engines have different capabilities. Only the simplest of regex can be shared across implementations.
If I recall correctly [:graph:] like specification of character classes is not supported in JS.

Related

regular expression matching filename with multiple extensions

Is there a regular expression to match the some.prefix part of both of the following filenames?
xyz can be any character of [a-z0-9-_\ ]
some.prefix part can be any character in [a-zA-Z0-9-_\.\ ].
I intentionally included a . in some.prefix.
some.prefix.xyz.xyz
some.prefix.xyz
I have tried many combinations. For example:
(?P<prefix>[a-zA-Z0-9-_\.]+)(?:\.[a-z0-9]+\.gz|\.[a-z0-9]+)
It works with abc.def.csv by catching abc.def, but fail to catch it in abc.def.csv.gz.
I primarily use Python, but I thought the regex itself should apply to many languages.
Update: It's not possible, see discussion with #nowox below.
I think your regex works pretty well. I recommend you to trying regex101 with your example:
https://regex101.com/r/dV6cE8/3
The expression
^(?i)[ \w-]+\.[ \w-]+
Should work in your case:
som e.prefix.xyz.xyz
^^^^^^^^^^^
some.prefix.xyz
^^^^^^^^^^^
abc.def.csv.gz
^^^^^^^
And in Python you can use:
import re
text = """some.prefix.xyz.xyz
some.prefix.xyz
abc.def.csv.gz"""
print re.findall('^(?i)[ \w-]+\.[ \w-]+', text, re.MULTILINE)
Which will display:
['som e.prefix', 'some.prefix', 'abc.def']
I might think you are a bit confused about your requirement. If I summarize, you have a pathname made of chars and dot such as:
foo.bar.baz.0
foobar.tar.gz
f.o.o.b.a.r
How would you separate these string into a base-name and an extension? Here we recognize some known patterns .tar.gz is definitely an extension, but is .bar.baz.0 the extension or it is only .0?
The answer is not easy and no regexes in this World would be able to guess the correct answer at 100% without some hints.
For example you can list the acceptable extensions and make some criteria:
An extension match the regex \.\w{1,4}$
Several extensions may be concatenated together (\.\w{1,4}){1,4}$
The remaining is called the basename
From this you can build this regular expression:
(?P<basename>.*?)(?P<extension>(?:\.\w{1,4}){1,4})$
Try this[a-z0-9-_\\]+\.[a-z0-9-_\\]+[a-zA-Z0-9-_\.\\]+

Regex Replacing characters with zero

I have the following string 3}HFB}4AF4}1 -M}1.
I have searched for this string using the regex :
([0-9])(\})([A-Z]{3})(\})([0-9][A-Z]{2}[0-9])(\})([0-9])(\s\-)([A-Z])(\})([0-9]).
I want to replace the } with 0. The Result I am looking for is 30HFB04AF401-M01, any assistance is appriciated. The tool I am using is Regex Buddy
A possible solution
Problem solved? In JavaScript at least :-)
"3}HFB}4AF4}1 -M}1".replace(/\}/g, "0");
// "30HFB04AF401 -M01"
I'm missing the point, right?
Assuming the language is JavaScript, we can write something like
"dfghj456783}HFB}4AF4}1 -M}1fghjkl8765".replace(/(?:[\d\w\s]+)([0-9]}[A-Z]{3}}[0-9][A-Z]{2}[0-9]}[0-9] -[A-Z]}[0-9])(?:[\d\w\s]+)/g, function () {
return arguments[1].replace(/}/g, "0");
});
What's possible in other languages though may be a different story.
Try the home of RegexBuddy for details.
So you've already got an expression to find instances of the string. Now you can either use groups to replace the characters, or you can use a separate regular expression over the string you found, simply replacing the } character within group(0) (which is the entire matched part of the input). I would certainly prefer the latter.
Fred seems to have created the replacement method for you already, so I won't repeat it here.
I have managed to find a solution to the formating in the JGSoft Lanugage used by Regex Buddy, thanks to all that provided suggestions that helped me channel my thoughts in the right direction.
Solution(I am still a beginner with Regex hence the syntax might not be efficent, but it does the job!!)
Using Group Names instead of Regex assiging groups with backreference and $ syntax.
Hence to replace 0 for } in the string 3}HFB}4AF4}1 -M}1 or any similar string. I used the following search and replacement syntax
Search : (?<Gp1>([0-9]))(?:})(?<Gp2>([A-Z]){3})(?:})(?<Gp3>([0-9])([A-Z]{2})([0-9]))(?:})(?<Gp4>([0-9]))(?:\s-)(?<Gp5>([A-Z]))(?:})(?<Gp6>[0-9])
Replace : ${Gp1}0${Gp2}0${Gp3}0${Gp4}-${Gp5}0${Gp6}
Result : 30HFB04AF401-M01

get data using regex

hello
i want to get data from a site using regex
http://helwa.maktoob.com/sec8180/art97048/pno1/title_%D8%B7%D8%A8%D9%82-%D9%81%D9%8A%D8%AA%D9%88%D8%AA%D8%B4%D9%8A%D9%86%D9%8A-%D8%A8%D8%A7%D9%84%D8%AE%D8%B6%D8%A7%D8%B1/index.htm
i used that regex /<div class="txtblk"(.*)?<div class="imgv cls">/is
but i gave me Invalid RegExp
why ?
i want to get data inside <div class="txtblk"></div>
Try escaping your double-quotes. Depending on your regex interpreter, those might be causing you problems.
The regex itself looks valid.
It depends on where/how you are using it, though; JavaScript for example doesn't know the /s modifier. To simulate a dot-matches-all mode in JavaScript, use [\s\S] instead of ..
Then, you might be running into problems with the quotes depending on the quoting rules for your language.
Also, you probably want to use (.*?) instead of (.*)?. (Or, if it's JavaScript, ([\s\S]*?)).
Finally, using regex to match HTML is not recommended. Use a DOM parser.
u may need to use a site that collects rss from links
like this
http://www.allwebdesignresources.com/webdesignblogs/graphics/turn-html-web-sites-into-rss-feeds-20-tools-converters-for-html-to-rss-conversions/

how to detect links in a string using RegEx in as3?

I am trying to find the generic links in strings. I've found a very handy regex on RegExr, in the community expressions:
(https?://)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?((/[a-zA-Z0-9_%]*)+)?(\.[a-z]*)?(:\d{1,5})?
I tried to use it and it returns null, although the same string tested on RegExr works fine:
var linkRegEx:RegExp = new RegExp("(https?://)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?((/[a-zA-Z0-9_%]*)+)?(\.[a-z]*)?(:\d{1,5})?","g");
var link:String = 'generic links: www.google.com http://www.google.com google.com';
trace(linkRegEx.exec(link));//traces null
Is there anything I'm missing ?
you need to double the backslashes when you're using new RegExp. you might want to use the literal syntax, which doesn't impose such a requirement (assuming AS3 admits this syntax, I just know JS.
Looks like maybe you're trying to match the wrong variable? In line linkRegEx.exec(formattedStatus), formattedStatus isn't defined.

Regex not returning 2 groups

I'm having a bit of trouble with my regex and was wondering if anyone could please shed some light on what to do.
Basically, I have this Regex:
\[(link='\d+') (type='\w+')](.*|)\[/link]
For example, when I pass it the string:
[link='8' type='gig']Blur[/link] are playing [link='19' type='venue']Hyde Park[/link]"
It only returns a single match from the opening [link] tag to the last [/link] tag.
I'm just wondering if anyone could please help me with what to put in my (.*|) section to only select one [link][/link] section at a time.
Thanks!
You need to make the wildcard selection ungreedy with the "?" operator. I make it:
/\[(link='\d+')\s+(type='\w+')\](.*?)\[\/link\]/
of course this all falls down for any kind of nesting, in which case the language is no longer regular and regexs aren't suitable - find a parser
Regular Expressions Info a is a fantastic site. This page gives an example of dealing with html tags. There's also an Eclipse plugin that lets you develop expressions and see the matching in realtime.
You need to make the .* in the middle of your regex non-greedy. Look up the syntax and/or flag for non-greedy mode in your flavor of regular expressions.