如何匹配可能被白色空间分解的确切字符串?

时间:2017-01-06 17:55:18

标签: .net regex powershell

我正在搜索可能被白色空格分解的精确字符串。有没有可以找到这个的正则表达式?我正在使用PowerShell / .NET

实施例: 查找' ABCDEFG'

// Should match
ABCDEFG

// Should not match
ABC4DEFG

// Should match
ABCD
EFG

// Should not match
A4BCD
EFG

我想出的最佳解决方案是读取字符串中的每个字符并跳过匹配的空格。我只是希望有更简单的答案。

编辑:不确定为什么这个问题会让人失望。仇恨会讨厌。

4 个答案:

答案 0 :(得分:5)

有时最简单的解决方案是不要在正则表达式中执行所有操作。 PowerShell示例:

$myString -replace '\s' -match 'ABCDEFG'

对于纯正则表达式,您希望用空格分隔每个字符:

A\s*B\s*D\s*E\s*F\s*G\s*

这样的事情可以在运行时在代码中生成。

$stringToMatch = 'ABCDEFG'
$regex = $stringToMatch -replace '\B', '\s*'

$myString -match $regex

答案 1 :(得分:0)

我认为你最好的答案是:

from selenium import webdriver
import time

links = ['https://www.youtube.com','https://www.google.com']

driver = webdriver.Chrome(R'C:\Users\a1234\Desktop\chromedriver.exe')

for link in links:
    driver.get(link)
    time.sleep(10)
    driver.close()

注意:如果字符串很长,你可以使用for循环在字符串的每个字母上构建这个循环。

答案 2 :(得分:0)

试试这个(在此测试:https://regex101.com/r/isXw1f/1

View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus){
                editText.getBackground().setColorFilter(ContextCompat.getColor(getActivity().getApplicationContext(),R.color.ed_background), PorterDuff.Mode.SRC_ATOP);
            } else {
                int color = Color.parseColor("#FFFFFF");
                editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            }
        }
    };

<color name="ed_background">#33FFFFFF</color>

必须将正则表达式设置为全局匹配

答案 3 :(得分:0)

您可以先使用\s*删除所有空格,然后匹配您需要的任何内容。取决于您打算如何处理匹配,如果需要空格,那么您将不得不找到其他内容。