re.match(r'。*',string)是否有限制?

时间:2018-10-17 14:50:06

标签: python regex

#! /usr/bin/python3

import re

my_string = 'This is the string to test.  It has several Capitalized words.  My name is Robert, and I am learning pYthon.'
result = re.match(r'.*', my_string)
result.group(0)

print(result)

请原谅我在发布此帖子时遇到的任何问题。我完全是菜鸟。我试图弄清楚为什么是当我运行上面的代码时, 我得到以下结果,而不是完整的字符串。

<_sre.SRE_Match object; span=(0, 108), match='This is the string to test.  It has several Capit>

谢谢。

2 个答案:

答案 0 :(得分:0)

您正在打印结果,而不是result.group(0)。随便

 @Override
public void onCreateContextMenu(ContextMenu contextMenu,View view,ContextMenu.ContextMenuInfo contextMenuInfo){
    super.onCreateContextMenu(contextMenu, view, contextMenuInfo);

    final WebView.HitTestResult webViewHitTestResult = webView1.getHitTestResult();

    if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
            webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

        contextMenu.setHeaderTitle("Menu");

        contextMenu.add(0, 1, 0, "Download this Image")
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {

                        String DownloadImageURL = webViewHitTestResult.getExtra();

                        if (URLUtil.isValidUrl(DownloadImageURL)) {

                            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));
                            request.allowScanningByMediaScanner();
                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                            DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                            assert downloadManager != null;
                            downloadManager.enqueue(request);

                            Toast.makeText(BrowserActivity.this, "Starting download...", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(BrowserActivity.this, "Sorry.. Something Went Wrong.", Toast.LENGTH_LONG).show();
                        }
                        return false;
                    }
                });
    }}

您将看到整个字符串。

答案 1 :(得分:0)

这是打印正则表达式匹配对象的怪癖。如果查看对象的span属性,则匹配从字符0到字符串的最后一个字符(108),并且如果按照{sergio的说明打印result[0],则得到的是整个字符字符串