简体   繁体   English

Python 特定字符之间的正则表达式捕获

[英]Python Regex Capture Between Specific Characters

I'm struggling with some regex for a few parts of these strings below.我正在为下面这些字符串的一些部分使用一些正则表达式。 This for use in a str.extract() and I need to capture:这用于 str.extract() ,我需要捕获:

  • jump or crawl, this will follow two spaces跳跃或爬行,这将跟随两个空格
  • valueA or valueB, this will follow the $ valueA 或 valueB,这将遵循 $
  • amount between @ and \n, sometimes, but not always, this includes up to two decimals @ 和 \n 之间的数量,有时但并非总是如此,这包括最多两位小数
⬆️  jump $valueA @ 5084\n\n#blah
⬆️  jump $valueB @ 628.15\n\n#blah
⬇️  crawl $valueB @ 626.8\n\n#blah
⬇️  crawl $valueB @ 4070\n\n#blah
⬆️  jump $valueA @ 6175.5\n\n#blah

You can use the pattern (jump|crawl)\s+\$(value[AB])\s@\s(\d*\.?\d*) :您可以使用模式(jump|crawl)\s+\$(value[AB])\s@\s(\d*\.?\d*)

df = pd.DataFrame({"value":["⬆️  jump $valueA @ 5084\n\n#blah",
                            "⬆️  jump $valueB @ 628.15\n\n#blah",
                            "⬇️  crawl $valueB @ 626.8\n\n#blah",
                            "⬇️  crawl $valueB @ 4070\n\n#blah",
                            "⬆️  jump $valueA @ 6175.5\n\n#blah"]})

print (df["value"].str.extract("(jump|crawl)\s+\$(value[AB])\s@\s(\d*\.?\d*)"))

       0       1       2
0   jump  valueA    5084
1   jump  valueB  628.15
2  crawl  valueB   626.8
3  crawl  valueB    4070
4   jump  valueA  6175.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM