首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用表达式查找响应中的文本

使用表达式查找响应中的文本
EN

Stack Overflow用户
提问于 2022-07-29 21:13:55
回答 2查看 53关注 0票数 0

我有这样的代码,我想用来检查域名注册:

代码语言:javascript
运行
复制
    private final static String WHO ="cnn.com";
    private final static String WHOIS_HOST = "whois.verisign-grs.com";
    private final static int WHOIS_PORT = 43;

    public static void main(final String[] args) throws IOException {
        SpringApplication.run(TestApplication.class, args);

        int c;
        Socket socket = null;

        String query = WHO + "\r\n";
        byte buf[] = query.getBytes();


        String regex = ".*Registry Expiry Date:*";

        try {
            socket = new Socket(WHOIS_HOST, WHOIS_PORT);
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();

            out.write(buf);
            out.flush();

            StringBuilder text = new StringBuilder();
            while ((c = in.read()) != -1) {
                System.out.print((char) c);
                text.append(c);
            }

            boolean matches = Pattern.matches(regex, text.toString());


            System.out.print("\nDone\n" + matches);
        } catch (IOException ex) {
             System.out.print(ex.getMessage());
        } finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException ex) {
                 }
            }
        }
    }

我得到了这个输出:

代码语言:javascript
运行
复制
   Domain Name: CNN.COM
   Registry Domain ID: 3269879_DOMAIN_COM-VRSN
   Registrar WHOIS Server: whois.corporatedomains.com
   Registrar URL: http://cscdbs.com
   Updated Date: 2018-04-10T16:43:38Z
   Creation Date: 1993-09-22T04:00:00Z
   Registry Expiry Date: 2026-09-21T04:00:00Z
   Registrar: CSC Corporate Domains, Inc.
   Registrar IANA ID: 299
   Registrar Abuse Contact Email: domainabuse@cscglobal.com
   Registrar Abuse Contact Phone: 8887802723
   Domain Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited
   Domain Status: serverDeleteProhibited https://icann.org/epp#serverDeleteProhibited
   Domain Status: serverTransferProhibited https://icann.org/epp#serverTransferProhibited
   Domain Status: serverUpdateProhibited https://icann.org/epp#serverUpdateProhibited
   Name Server: NS-1086.AWSDNS-07.ORG
   Name Server: NS-1630.AWSDNS-11.CO.UK
   Name Server: NS-47.AWSDNS-05.COM
   Name Server: NS-576.AWSDNS-08.NET
   DNSSEC: unsigned
   URL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/
>>> Last update of whois database: 2022-07-29T20:55:54Z <<<

For more information on Whois status codes, please visit https://icann.org/epp

NOTICE: The expiration date displayed in this record is the date the
registrar's sponsorship of the domain name registration in the registry is
currently set to expire. This date does not necessarily reflect the expiration
date of the domain name registrant's agreement with the sponsoring
registrar.  Users may consult the sponsoring registrar's Whois database to
view the registrar's reported date of expiration for this registration.

TERMS OF USE: You are not authorized to access or query our Whois
database through the use of electronic processes that are high-volume and
automated except as reasonably necessary to register domain names or
modify existing registrations; the Data in VeriSign Global Registry
Services' ("VeriSign") Whois database is provided by VeriSign for
information purposes only, and to assist persons in obtaining information
about or related to a domain name registration record. VeriSign does not
guarantee its accuracy. By submitting a Whois query, you agree to abide
by the following terms of use: You agree that you may use this Data only
for lawful purposes and that under no circumstances will you use this Data
to: (1) allow, enable, or otherwise support the transmission of mass
unsolicited, commercial advertising or solicitations via e-mail, telephone,
or facsimile; or (2) enable high volume, automated, electronic processes
that apply to VeriSign (or its computer systems). The compilation,
repackaging, dissemination or other use of this Data is expressly
prohibited without the prior written consent of VeriSign. You agree not to
use electronic processes that are automated and high-volume to access or
query the Whois database except as reasonably necessary to register
domain names or modify existing registrations. VeriSign reserves the right
to restrict your access to the Whois database in its sole discretion to ensure
operational stability.  VeriSign may restrict or terminate your access to the
Whois database for failure to abide by these terms of use. VeriSign
reserves the right to modify these terms at any time.

The Registry database contains ONLY .COM, .NET, .EDU domains and
Registrars.

Done
false

你知道我怎样才能得到行内容Registry Expiry Date: 2026-09-21T04:00:00Z

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-29 22:27:31

当您将字节值附加到StringBuilder中时,存在一个问题,因此,如果像这样更改,则匹配器无法工作,您可以得到所需正则表达式的值。

代码语言:javascript
运行
复制
        StringBuilder text = new StringBuilder();
        while ((c = in.read()) != -1) {
           // System.out.print((char) c);
            text.append((char) c);
        }
        System.out.println(text);
        String regex = ".*Registry Expiry Date.*Z";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text.toString());
        if (matcher.find())
        {
            System.out.println("\nDone\n" + matcher.group(0));
        }

在这种情况下,结果将是

代码语言:javascript
运行
复制
Done
   Registry Expiry Date: 2026-09-21T04:00:00Z
票数 1
EN

Stack Overflow用户

发布于 2022-07-29 23:19:41

有几件事需要清理:

  • c被定义为int,所以当您调用text.append(c)时,就会追加一个整数值--为了解决这个问题,您可以将"c“转换为一个字符:text.append((char) c)
  • your代码将整个响应读入一个StringBuilder,然后处理该文本,然后查找任何潜在的匹配项--这并不是一个巨大的低效率,但也没有必要;相反,您可以检查整个过程中的数据,看看是否遇到了数据中有趣的部分,如果是的话,跳过使用模式和Matcher处理rest
  • 是可以的,但是对于您介绍的情况,它是额外的复杂性。

以下是一个解决方案:

resources

  • Wraps

  • 打开套接字并在一个带资源的尝试块中"out“,这样,两条都将被自动关闭(更简单的代码)

  • 会在一个具有资源的尝试块中打开输入流--再次,更少的代码,自动管理打开的BufferedReader中的输入流--这允许您在way循环中按行

  • 读取输入行,而不是使用模式和Matcher,它只是检查每一行文本是否包含”注册表到期日期“

  • ,如果找到匹配,它会打印匹配,然后中断循环--不需要查看任何更多的输入数据

代码语言:javascript
运行
复制
String WHO = "cnn.com";
String WHOIS_HOST = "whois.verisign-grs.com";
int WHOIS_PORT = 43;

try (Socket socket = new Socket(WHOIS_HOST, WHOIS_PORT)) {
    try (OutputStream out = socket.getOutputStream()) {
        out.write((WHO + "\r\n").getBytes());
        out.flush();

        try (BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
            String line;
            while ((line = input.readLine()) != null) {
                if (line.contains("Registry Expiry Date")) {
                    System.out.println("---> " + line);
                    break; // don't need to read any more input
                }
            }
        }
    }

} catch (Exception e) {
    e.printStackTrace();
}

这是输出:

代码语言:javascript
运行
复制
--->    Registry Expiry Date: 2026-09-21T04:00:00Z
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73171051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档