查找正则表达式链接 - Java

时间:2015-10-16 18:56:03

标签: java regex

我想通过regexp找到网页上的链接(保存在String变量中)。特别是由标签定义:&#34; <a href=”link”></a>&#34;。 (从<a href=开始并结束</a>)如何看起来像这个正则表达式,我应该输入什么?领域。 TIA;)

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {
public static void main(String[] args) {

    String sourceOfHtml = "Some html code of webpage with links";


    regexChecker("???", sourceOfHtml);


}

public static void regexChecker(String theRegex, String str2check){

    Pattern checkRegex = Pattern.compile(theRegex);

    Matcher regexmatcher = checkRegex.matcher(str2check);

    while(regexmatcher.find()){
        if(regexmatcher.group().length()!=0){
            System.out.println(regexmatcher.group().trim());

        }


    }
}

}

2 个答案:

答案 0 :(得分:1)

不要使用正则表达式。 Do use an HTML parser.

int main(){
sqlite3 *db;
int     rc;
char filename[512];
char query[512];
char trace_string[512];
sqlite3_stmt *Stmt;
const char *zLeftover;

strcpy(filename,"/data/xcd/textdatabase/2015274/2015274_mos.sqlite");
strcpy(query,"ATTACH '/data/xcd/textdatabase/2015273/2015273_mos.sqlite' as db1");

rc = sqlite3_open_v2(filename, &db,SQLITE_OPEN_READONLY,NULL);
if (rc != SQLITE_OK) {
    sprintf(trace_string,"open_database: failed to open %s, rc=%d\n", filename, rc);
    fprintf(stdout,trace_string);
    return 0;
}
else {
    sprintf(trace_string,"open_database: %s opened rc=%d\n", filename, rc);
    fprintf(stdout,trace_string);
}

rc = sqlite3_prepare(db, query, -1, &Stmt, &zLeftover);
if (rc != SQLITE_OK) {
    sprintf(trace_string,"test: sqlite3_prepare, command=%s, rc=%d\n", query, rc);
    fprintf(stdout,trace_string);
    return rc;
}

rc = sqlite3_step(Stmt);
if ((rc != SQLITE_OK) && (rc != SQLITE_DONE) && (rc != SQLITE_ROW)) {
    sprintf(trace_string,"test: sqlite3_step, command=%s, rc=%d\n", query, rc);
    fprintf(stdout,trace_string);
    return rc;
}

rc = sqlite3_finalize(Stmt);

sprintf(trace_string,"test: sqlite3_finalize, command=%s, rc=%d\n", query, rc);
fprintf(stdout,trace_string);

rc=sqlite3_close(db);
sprintf(trace_string,"close_database: rc=%d\n", rc);
fprintf(stdout,trace_string);
return (rc);

答案 1 :(得分:0)

你可以试试这个正则表达式:)

{
    "kind":"Service",
    "apiVersion":"v1",
    "metadata":{
        "name":"check",
        "labels":{
            "app":"check"
        }
    },
    "spec":{
       "type": "LoadBalancer",
       "ports": [
       {
           "port":80,
           "name":"check-server"
       }
       ],
       "selector":{
           "app":"check"
       }
    }
}