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 🙂

Advertisements
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

Posted in Uncategorized

[Cordova] How to preview PDF file inside Cordova project

cordova_logo

Hi all, I faced this challenge for my tasks list. After searching a lot on Internet with various solutions:

  • Using InAppBrower: this plugin can open PDF for preview but it will open a certain PDF reader application on device not inside our Cordova project
  • PDFObject: hmmm, I tried use it but I faced compile error about import it into my project, I did not fix that issue because of time limitation.

Finally, I found the solution which solved my challenge 🙂 (it saved my life 🙂 ).

Below is result:

screenshot_2016-10-18-14-25-43

About the detail, please follow steps of the “example” at my github

I preferred that solution at sayanee’s github. Thank him very much 🙂

You also install that plugin by npm (Ref detail at here):

npm install angular-pdf

Hope it is helpful 🙂

Posted in Cordova/PhoneGap

[Cordova] How to download a file

cordova_logo

To download a file from given URL, we need the cordova-plugin-file-transfer plugin. You also install this plugin by command:

cordova plugin add cordova-plugin-file-transfer

Then, use below code:

 function downloadFile(downloadedFileName, downloadLink) {

var fileURL;
if (monaca.isAndroid === true) {
var androidDownloadPath = ‘/storage/emulated/0/path/to/’;
fileURL = androidDownloadPath + downloadedFileName;
} else {
var iosDownloadPath = cordova.file.documentsDirectory;
fileURL = iosDownloadPath + downloadedFileName;
}
var uri = encodeURI(downloadLink);
processDownloadFile(uri, fileURL);

}

function processDownloadFile(uri, fileURL) {
var encodedFileURL = encodeURI(fileURL);
var fileTransfer = new FileTransfer();
fileTransfer.download(
uri,
encodedFileURL,
function(entry) {
console.log(“Download complete ! Place of downloaded file: ” + entry.toURL() + ” – Source: ” + uri);
},
function(error) {
console.error(“error source ” + error.source + ” – download error target ” + error.target + ” – upload error code” + error.code);
},
false, {
headers: {
“Authorization”: “Basic false headers”
}
}
);
}

Posted in Android Programming, Cordova/PhoneGap, iOS Programming, Mobile_Tips

[Cordova] Tips: How to delete a file

cordova_logo

Actually, it is very easy and simple because you can use the cordova-plugin-file

You can download the file plugin with command:

cordova plugin add cordova-plugin-file

Demo code:

var path = "file:///storage/emulated/0";
var filename = "myfile.txt";

window.resolveLocalFileSystemURL(path, function(dir) {
	dir.getFile(filename, {create:false}, function(fileEntry) {
              fileEntry.remove(function(){
                  // The file has been removed succesfully
              },function(error){
                  // Error deleting the file
              },function(){
                 // The file doesn't exist
              });
	});
});

Hope it is useful🙂