首页 \ 问答 \ 字符串简单替换(String Simple Substitution)

字符串简单替换(String Simple Substitution)

我将大多数用户使用的简单正则表达式格式转换为正确的re python正则表达式字符串的最简单方法是什么?

作为一个例子,我需要转换这个:

string = "*abc+de?"

对此:

string = ".*abc.+de.?"

当然,我可以遍历字符串并按字符构建另一个字符串,但这肯定是一种低效的方法吗?


What's the easiest way of me converting the simpler regex format that most users are used to into the correct re python regex string?

As an example, I need to convert this:

string = "*abc+de?"

to this:

string = ".*abc.+de.?"

Of course I could loop through the string and build up another string character by character, but that's surely an inefficient way of doing this?


原文:https://stackoverflow.com/questions/217881
更新时间:2022-01-20 21:01

最满意答案

这是你想要做的?:

@Echo OFF

:: By Elektro H@cker

Set "folder=C:\Users\Administrador\Desktop\Test"

Echo Selected Folder: "%FOLDER%"
Set /P "files=Select files to merge (Example: "File1.txt" "File2.txt" "File3.txt") >> "


For %%@ in (%files%) Do (
    For /R "%FOLDER%" %%# in (*) DO (
        IF /I "%%@" EQU "%%~nx#" (call Set "Files_to_merge=%%Files_to_merge%% "%%#"+")
        )
    )
)


Copy /A %Files_to_merge:~0,-1% ".\output_file.txt"
pause&Exit

PS:请记住更改复制命令的格式,/ A表示文本文件,/ B表示二进制文件。

CMD输出:

>Selected Folder: "C:\Users\Administrador\Desktop\Test"
>Select files to merge (Example: "File1.txt" "File2.txt" "File3.txt") >>

>file1.txt file2.txt file3.txt

C:\Users\Administrador\Desktop\Test\file1.txt
C:\Users\Administrador\Desktop\Test\Nueva carpeta\file1.txt
C:\Users\Administrador\Desktop\Test\file2.txt
C:\Users\Administrador\Desktop\Test\Nueva carpeta\file2.txt
C:\Users\Administrador\Desktop\Test\file3.txt
C:\Users\Administrador\Desktop\Test\Nueva carpeta\file3.txt
        1 file(s) copied(s).

Presione una tecla para continuar . . .

Output_file.txt内容:

Content of "File1.txt" in first dir

Content of "File1.txt" in firstsub-dir

Content of "File2.txt" in first dir

Content of "File2.txt" in firstsub-dir

Content of "File3.txt" in first dir

Content of "File3.txt" in firstsub-dir

This is what you want to do?:

@Echo OFF

:: By Elektro H@cker

Set "folder=C:\Users\Administrador\Desktop\Test"

Echo Selected Folder: "%FOLDER%"
Set /P "files=Select files to merge (Example: "File1.txt" "File2.txt" "File3.txt") >> "


For %%@ in (%files%) Do (
    For /R "%FOLDER%" %%# in (*) DO (
        IF /I "%%@" EQU "%%~nx#" (call Set "Files_to_merge=%%Files_to_merge%% "%%#"+")
        )
    )
)


Copy /A %Files_to_merge:~0,-1% ".\output_file.txt"
pause&Exit

PS: Remember to change the format of Copy command, /A for text files, /B for binary files.

CMD Output:

>Selected Folder: "C:\Users\Administrador\Desktop\Test"
>Select files to merge (Example: "File1.txt" "File2.txt" "File3.txt") >>

>file1.txt file2.txt file3.txt

C:\Users\Administrador\Desktop\Test\file1.txt
C:\Users\Administrador\Desktop\Test\Nueva carpeta\file1.txt
C:\Users\Administrador\Desktop\Test\file2.txt
C:\Users\Administrador\Desktop\Test\Nueva carpeta\file2.txt
C:\Users\Administrador\Desktop\Test\file3.txt
C:\Users\Administrador\Desktop\Test\Nueva carpeta\file3.txt
        1 file(s) copied(s).

Presione una tecla para continuar . . .

Output_file.txt contents:

Content of "File1.txt" in first dir

Content of "File1.txt" in firstsub-dir

Content of "File2.txt" in first dir

Content of "File2.txt" in firstsub-dir

Content of "File3.txt" in first dir

Content of "File3.txt" in firstsub-dir

相关问答

更多
  • 我很长时间没有完成C ++,但是从这篇文章看来,这是一种性能技巧,可以停止重新使用常见标头的符号。 您可以尝试/ Z7将信息嵌入到每个obj中,而不是创建PDB,然后使用rebase链接并重新创建它,如本文中所述 。 I've not done C++ for a long time but from this article, it appears that this is a performance trick to stop the recreation of symbols for common h ...
  • 来自patchutils的 combinediff可以结合你的差异。 (从以前的SO问题中无耻地借用过 。) combinediff from patchutils can combine the diffs for you. (Shamelessly borrowed from this previous SO question.)
  • 考虑到pdfunite是poppler的一部分,它有更高的安装机会,使用也比pdftk简单: pdfunite in-1.pdf in-2.pdf in-n.pdf out.pdf I'm sorry, I managed to find the answer myself using google and a bit of luck : ) For those interested; I installed the pdftk (pdf toolkit) on our debian server, an ...
  • 这是你想要做的?: @Echo OFF :: By Elektro H@cker Set "folder=C:\Users\Administrador\Desktop\Test" Echo Selected Folder: "%FOLDER%" Set /P "files=Select files to merge (Example: "File1.txt" "File2.txt" "File3.txt") >> " For %%@ in (%files%) Do ( For /R "%F ...
  • 如果你想将XSLT应用于多个XML文档(使用纯XSLT,我不知道Stax),你可以简单地使用document函数(XSLT 1.0和2.0)或collection函数(使用XSLT 2.0),例如 < ...
  • 你需要一个Map 。 字符串是.properties文件名,属性是从文件中读取的内容。 递归地使用File Visitor查找属性文件。 对于每个找到的文件加载,它会更新映射中的旧内容(如果已找到这样的文件名)。 处理完所有文件后,迭代所有文件名(映射中的键),并为每个文件保存一个新的属性文件,其中包含从所有找到的文件收集的内容。 You need a Map. is the .properties filename and is ...
  • 一些观察: 在某些时候, opendir($csvdir)也将包含你的merge.csv文件。 你想避免这种情况。 保持独立。 保持文件操作最小化。 对于循环中的每个.csv文件,您都要写入merge.csv 。 相反,收集数据以在循环中写入并仅写入一次。 你在file_get_contents($file);得到了错误的文件内容file_get_contents($file); 。 $file只包含文件名,而不是完整路径。 $test从未使用过。 这是工作代码:
  • 有很多方法可以解决这个问题。 默认情况下,plist是XML文件的特殊形式。 如果你弄清楚实际上可以使用文本编辑器将多个文件的内容合并到一起的语法,但是你需要确保你正确地做到了。 plist文件具有整个文件的特定标题。 你不能复制/粘贴多个plists在一起,因为那样他们会重复这个标题。 接下来的方法是以编程方式。 如果你能弄清楚这些文件包含的外部集合的类型(可能是一个数组或字典),那么你可以写几行代码作为数组读取每一个plists,使用NSArray代码将它们结合起来(假设它们包含数组的颜色),然后将组合 ...
  • 你有几个选择: 如果您知道要合并的更改集(或要避免合并的更改集),则可以从要合并的文件中选择结构中的文件夹,然后通过选择所需的特定更改集来执行合并。 如果您有变更集2000,2001,2004,2010并且您想要2001和2010,这有点复杂。您只能在每个合并的列表中合并连续的变更集,因此您必须合并2001年和2010年。这仍然比合并更好如果您有更多文件然后更改组,则会有许多文件。 如果要将该文件夹下的所有内容合并到特定变更集或最新变更集,则可以在一次批量合并中将其合并。 在这两个方面,您仍然需要解决在源和 ...
  • 如果你在某种Unix上,你可以使用tail 。 $ tail -qn +2 fileA fileB ... -q压缩输出中的文件名; -n +2从第2行开始输出。 要获得标题: $ (head -n 1 fileA; tail -qn +2 fileA fileB ...) > output-file 如果你需要使用Perl: use strict; use warnings; use autodie; my $files = $#ARGV; # get number of files - 1 whil ...

相关文章

更多

最新问答

更多
  • 以编程方式创建视频?(Create videos programmatically?)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • javascript数组,如何改变这个数组结构(javascript arrays, how to change this array structure)
  • 在ASP.NET Web API中使用多个Get方法进行路由(Routing with multiple Get methods in ASP.NET Web API)
  • 用于backbone.js验证的自定义验证器(Custom validator for backbone.js validation)
  • const char *与其他指针有什么不同?(Is const char * different from other pointers? [duplicate])
  • 无效的列索引,使用PreparedStatement更新(Invalid column index , update using PreparedStatement)
  • watchOS WCSession'已配对'和'watchAppAvailable'不可用(watchOS WCSession 'paired' and 'watchAppAvailable' are unavailable)
  • CalledFromWrongThreadException在Android上执行JUnit测试(CalledFromWrongThreadException exercising JUnit tests on Android)
  • 如何把文件保存到你的应用程序目录中?(How to put\save files into your application directory? (adobe air))
  • 美元符号在Java方法描述符中的含义?(Meanings of dollar sign in Java method descriptor?)
  • font-size的含义是什么:1em / 2em?(What doe the meaning of font-size:1em/2em?)
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • Android - 检测与特定wifi ssid断开连接的正确方法?(Android - Correct way to detect disconnecting from a particular wifi ssid?)
  • 通过Shell脚本将文件转换为另一个文件(Convert File To Another File By Shell Script)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • 如何过滤magento废弃的购物车报告集合(How to Filter the magento abandoned cart report collection)
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • web api http post传递对象504接收失败(web api http post passing object 504 Receive Failure)
  • Rails从视图编辑模型上的多个属性的方法(Rails way to edit multiple attributes on a model from a view)
  • 总是用{}初始化对象是否是好习惯?(Is it good habit to always initialize objects with {}?)
  • 在方案中编写特殊字符到输出端口(编译器设计)(writing special characters to output port in scheme (compiler design))
  • 电脑等级考试得证有多大用处?
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • 第一次调用函数将无法按预期工作,但下一次工作正常(calling a function on the first time won't work as expected, but next time is working)
  • 如何优化使用BigInteger操作执行时间的代码(How to optimize the code that uses BigInteger operations for execution time)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何提供个人资料信息,以便Passport.js可以使用它?(how does Profile information should be provided so Passport.js can use it?)
  • 有没有办法初始化jquery数据表中的细节?(is there any way to initialize details in jquery datatable?)