Posted in Uncategorized

[Java] Regular Express for URLs detection in a sentence.

java_logo_100

Regular Express is very great. It helps us a lot in cases of filtering string which we expect. In this section, I want to introduce once case using Regex, that is detecting URL links in a sentence.

To detect valid URLs which start with “www.”, “http” and “https” in a sentence, you can use below Regular Express:

public List<String> extractUrls(String text)
{
List<String> containedUrls = new ArrayList<String>();

// String urlRegex = “((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)”;
String urlRegex =
“(”
+ “(”
+ “((https?|ftp|gopher|telnet|file):((//)|(\\\\)))” // URLs starting with http://, https://, or ftp://
+ “|”
+ “(^|[^\\/])(www\\.)” // URLs starting with “www.” (without // before it, or it’d re-link the ones done above).
+ “)”
+ “+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*”
+ “)”;
Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);

Matcher urlMatcher = pattern.matcher(text);
while (urlMatcher.find())
{
containedUrls.add(text.substring(urlMatcher.start(0),
urlMatcher.end(0)));
}

return containedUrls;
}

Above Regular Express only detect which word contain URL(s) such as: “<www.google.com<>>>aaa”, etc. So, to remove unexpected other words, you can modify code to do that.

Please refer my_demo to remove unexpected words after filtering using Regular Express

Example:

Hope it help 🙂

Posted in JavaScript

[Javascript] Regular Express for URL and Email detection in a sentence.

javascript

There are many ways to detect URL(s) in a sentence:

  • Use validUrl lib: Refer detail at here

This lib has a disadvantage that is it can analyze a URL is valid or not. It means if you put that URL in a sentence such as “Hello http://www.saothienhat.wordpress.com site !”, it cannot detect the URL

  • Use below Regular Express:
function linkify(inputText) {
    var replacedText, replacePattern1, replacePattern2, replacePattern3;

    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    //Change email addresses to mailto:: links.
    replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
    replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText;
}

Using above Regular Express, you can detect URL(s) as well as Email(s) in a sentence. You can modify this Regular Express for your jobs :).

You can refer detail of Regular Express guide at here

Like and Share if it is helpful 🙂

Posted in Cordova/PhoneGap

[Cordova] Display Onsen UI popover as dropdown

It is very easy to display an Onsen UI popover as dropdown.

For full example, please refer here

Below is detail of code to display popover using Onsen UI

  • HTML
<body ng-controller="DropdownController">
<ons-page>
  <ons-toolbar>
Dropdown example
</ons-toolbar> </ons-page> <ons-template id="popover.html"> <ons-popover cancelable direction="down"> <ons-list> <ons-list-item ng-repeat="option in options" modifier="tappable"> {{ option }} </ons-list-item> </ons-list> </ons-popover> </ons-template> </body>
  • JS
.controller('DropdownController', function($scope) {
  ons.ready(function() {
    ons.createPopover('popover.html').then(function(popover) {
      $scope.popover = popover;
    });
  });

  $scope.options = ['Load', 'Sync', 'Settings'];
});
Posted in Mobile_Tips, Useful Apps

[App] JLT Free – Mobile app provide a huge of Japanese tests for learners

 

jlt_icon

JLT – Japanese Language Test is a Mobile application which provide a lot of Test data for learner on N1, N2, N3, N4, N5 levels.

–          It is FREE

–          Easy to use, UI is very friendly.

–          It provice a lot of Japanese tests.

–          Test Result Analysis is clear.

–          Daily update.

How to download for Android ?

–          Way 1: Open “Google Play” on your mobile and search “JLT Free”, then install it.

–          Way 2: Get it from link: https://play.google.com/store/apps/details?id=com.saothienhat.jltfree&hl=en