返回子字符串匹配vimscript

时间:2018-10-03 05:36:05

标签: vim

我想使用带有正则表达式的vimscript提取markdown链接的网址。

理想情况是这些行中的内容:

package StudentBody;

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

    Address school = new Address("800 Lancaster Ave.", "Villanova", "PA", 19085);
    Address jHome = new Address("21 Jump Street", "Blacksburg", "VA", 24551);

    Student john = new Student("John", "Smith", jHome, school);

    Address mHome = new Address("123 Main Street", "Euclid", "OH", 44132);
    Student marsha = new Student("Marsha", "Jones", mHome, school);

    System.out.println(john);
    System.out.println();
    System.out.println(marsha);

    }
}

public class Student {

private String firstName, lastName;
private Address homeAddress, schoolAddress;
private double score1, score2, score3;
private int testnumber;

public Student(String first, String last, Address home, Address school)
{
    firstName = first;
    lastName = last;
    homeAddress = home;
    schoolAddress = school;
    score1 = 0;
    score2 = 0;
    score3 = 0;
    testnumber = 0;
}
public Student(double score1_, double score2_, double score3_, int testnumber_)
{
    score1 = score1_;
    score2 = score2_;
    score3 = score3_;
    testnumber = testnumber_;
}
public void setTestScore(double score1_1, double score2_1, double score3_1, int testnumber1_1)
{
    score1 = score1_1;
    score2 = score2_1;
    score3 = score3_1;
    testnumber = testnumber1_1;
}
public double getTestScore()
{

}
public String toString()
{
    String result;

    result = firstName + " " + lastName + "\n";
    result += "Home Address:\n" + homeAddress + "\n";
    result += "School Address:" + schoolAddress;

    return result;
    }
}


public class Address {  
private String streetAddress, city, state;
private long zipCode;

public Address(String street, String town, String st, long zip)
{
    streetAddress = street;
    city = town;
    state = st;
    zipCode = zip;
}
public String toString()
{
    String result;

    result = streetAddress + "\n";
    result += city + ", " + state + " " + zipCode;

    return result;
}

}

给定一个字符串,例如:fun! GetLinkUri(str) return match(a:str, '[.*]\((.*)\)', \1) endfunc 它将返回The search engine [Google](https://google.com) blabla

函数描述的方式不是正确使用match。有匹配的方法吗?还有其他功能吗?

1 个答案:

答案 0 :(得分:5)

您将需要使用matchstr而不是match。但这不是您遇到的唯一问题。我会这样做:

return matchstr(a:str, '\[.*\](\zs.*\ze)')
  1. [.*]表示匹配一个字符,即'.''*'。如果要匹配文字方括号,则需要转义方括号。

  2. 不幸的是,matchstr无法返回单个子组,因此我使用\zs\ze将匹配部分限制在括号之间。

相关问题