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

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.

Related

regex change to detetct more pre-domain

I am new to regular expression and trying to learn changing the following regex, was wondering if you can help me.
The following will detect for me the links such as :
<http://www.ijs.si/software/delet.obo#VO_Broker>
regex:
<(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})
How do I change the same regex so it can also detect for me
<http://kt.ijs.si/software/delet.obo#VO_Broker>
I use the following regex to check for urls:
(\b(https?|ftp|file):\/\/[-\w+&##\/%?=~_|!:,.;]*[-\w+&##\/%=~_|])
Works fine and is a lot simpler.

Regular Expression is working for php not for JavaScript

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.

Why won't this regexp work in google spreadsheets?

I'm trying to extract from a url using a regexp in google spreadsheets. However the spreadsheet returns #VALUE! with the following error: Invalid regular expression: invalid perl operator: (?<
Here is the regexp I'm using: (?<=raid_boss=)[a-zA-Z0-9_]+
A sample url will contain a variable in it that says raid_boss=name. This regexp should extract name. It works in my testing program, but not in google spreadsheet.
Here is the exact contents of the cell in google spreadsheets: =REGEXEXTRACT( B1 ; "/(?<=raid_boss=)[-a-zA-{}-9_]+" )
Any insight or help would be much appreciated, thank you!
Sounds like whatever regular-expression engine Google Docs is using doesn't support lookbehind assertions. They are a relatively rare feature.
But if you use captures, REGEXEXTRACT will return the captured text, so you can do it that way:
=REGEXEXTRACT( B1 ; "raid_boss=([a-zA-Z0-9_]+)" )
Javascript is not the issue - Google Sheets uses RE2 which lacks lookbehind
along with other useful things.
You could use:
regexextract(B1, ".*raid_boss=(.*)")
or else native sheet functions like FIND, SUBSTITUTE if that isn't working
Finding a good regex testing tool is tricky - for example you can make something that works in http://rubular.com/ but fails in GSheets. You need to make sure your tool supports the RE2 flavour eg: https://regoio.herokuapp.com/

Actionscript regular expressions

I have written the following regular expression:
[\s\S]*?,[\s\S]*?,(?P<lat>[\s\S]*?),(?P<long>[\s\S]*?)
In java it worked like a charm.
But when i use it in AS3 like this:
var pattern:RegExp = [\s\S]*?,[\s\S]*?,(?P<lat>[\s\S]*?),(?P<long>[\s\S]*?);
it gives an error...
I thought regular expressions where language independent...
Any help?
Thanks in advance.
I believe you need to add / delimiters to the front and back side of you regex.
What is an example string you are trying to use, and what is the desired output?
And what is the error you get?

Regexp to get domain from URL

Anyone knows the Regexp for AS3 to get the domain name from URL?
E.g: "http://www.google.com/translate" will get "http://www.google.com"
There's is a very complete utility class for dealing with URIs in as3corelib. Perhaps you might want to use this instead of rolling your own.
import com.adobe.net.URI;
var uri:URI = new URI("http://www.google.com/translate");
trace(uri.authority); // traces www.google.com
This should do the work: http(s?)://([\w]+\.){1}([\w]+\.?)+
You can try this in GSkinner RegExr
Try this one:
.*\/\/([^\/:]+).*
Surrounds the regexp by the preferred separators, which might depend on language.
This diagram explain how it works.
The previous regex provided missed some url types i needed, so for the benefit of googlers, I use
(http://|https://)?(www.)?[A-z]*(.com|.co.uk|.us|.org|.net|.mobi)
in C#
var regexMatch = Regex.Match(input, #"(http://|https://)?(www.)?[A-z]*(.com|.co.uk|.us|.org|.net|.mobi)", RegexOptions.Multiline);
string domain = regexMatch.Value;
See example at RegExr.
You can remove remove "(...)?" parts to stop that part appearing the match, for example to remove the http matching that I require.
Probably not perfect, but works for me. This site is an excellent reference for building up expressions.
Regex to get the domain from the window.location.href property is provided below. Modifications have to made to a regex answer provided previously by OXMO456 to make it correct and accommodate more scenarios.
domainRegex = /(http(?:s?):\/\/(?:[\w]+(?:\.|\:)){1}(?:[\w]+\.?)+)/gi;
Regexr link: https://regexr.com/4vnnc