You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python正则表达式:如何匹配RANDOM与斜杠间的非数字字符?

How to Extract Non-Digit Characters Between "RANDOM" and Forward Slash

Hey there! Let's solve this regex problem for you. I'll break it down with clear examples you can test directly in Pythex.

First, let's align on the goal

You want to grab all non-digit characters that sit between the exact string RANDOM and the first forward slash (/) in your target text. For example:

  • If your input is testRANDOMabc123def/end, you want to extract abcdef
  • If your input is RANDOMxyz/123, you want xyz

Solution 1: Two-step approach (simplest for Pythex testing)

This method is easy to understand and verify:

  1. First, isolate the entire substring between RANDOM and the first /
  2. Strip all digits from that substring

Step 1: Regex to get the middle segment

Use this pattern to capture everything between RANDOM and the first slash:

(?<=RANDOM).*?(?=/)
  • (?<=RANDOM): Positive lookbehind to ensure we’re positioned right after RANDOM
  • .*?(?=/): Non-greedy match of any characters until we hit the first /

Step 2: Remove digits from the result

Take the matched text (e.g., a1b2c) and use this regex to delete all digits:

\d

In Pythex, paste the middle segment into the "Test String" field, enter the regex above, and use an empty string as the replacement. You’ll get your desired non-digit characters.


Solution 2: Single regex to extract non-digits directly

If you want to get the final result in one go (using Python’s regex engine, which Pythex supports), use this pattern to find all non-digit sequences between RANDOM and /:

(?<=RANDOM)(?:(?!/)\D)+
  • (?<=RANDOM): Positions us after RANDOM
  • (?:(?!/)\D)+: Matches one or more non-digit characters (\D) that aren’t followed by a slash ((?!/))

When you test this in Pythex, it will directly highlight all non-digit characters between RANDOM and the first slash.


Edge Cases to Keep in Mind

  • If there are no non-digit characters between RANDOM and / (e.g., RANDOM123/), the result will be an empty string
  • This targets the first slash after RANDOM (thanks to non-greedy matching)
  • If RANDOM appears multiple times, it will match the first occurrence and its following slash unless you adjust the regex to target a specific instance

Let me know if you need to tweak this for your exact input strings!

内容的提问来源于stack exchange,提问作者Rdrr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:11:21