2
import re

value = "world_wide='test1/one/two', " \
        "stage_test='ALPHA', world_wide='test2/one/two', " \
        "stage_test='GAMMA', world_wide='test3/one/two', " \
        "stage_test='GAMMA', world_wide='test4/one/two', " \
        "stage_test='ALPHA', world_wide='test5/one/two', " \
        "stage_test='GAMMA', world_wide='test6/one/two', " \
        "stage_test='GAMMA"

pattern = r"(world_wide=\'.*\')"

for match in re.findall(pattern, str(value)):
    print ("\n", match)

Trying to filter for a specific output given a string value. With the code above the following output is given:

 world_wide='test1/one/two', stage_test='ALPHA', world_wide='test2/one/two', stage_test='GAMMA', world_wide='test3/one/two', stage_test='GAMMA', world_wide='test4/one/two', stage_test='ALPHA', world_wide='test5/one/two', stage_test='GAMMA', world_wide='test6/one/two', stage_test='

What I'm trying to get is: if the string matches a specific condition such as:

if 'world_wide=' is found, return the following value between the two characters. In this case, this would be two single quotes excluding the '/one/two'.

Desired output:

>>>test1
test2
test3
test4
.........
2
  • 1
    hello welcome to Stack Overflow! Could you reformat your question so that in the input string value is more readable? It seems like you have a quote closing problem in your post. Commented Jun 4, 2018 at 4:25
  • corrected the formatting a bit to make it more readable Commented Jun 4, 2018 at 4:48

4 Answers 4

1

You could use the following expression:

world_wide='([^/]+)
# world_wide='
# capture anything not a / into group 1


In Python this is:

import re

value = "world_wide='test1/one/two', " \
        "stage_test='ALPHA', world_wide='test2/one/two', " \
        "stage_test='GAMMA', world_wide='test3/one/two', " \
        "stage_test='GAMMA', world_wide='test4/one/two', " \
        "stage_test='ALPHA', world_wide='test5/one/two', " \
        "stage_test='GAMMA', world_wide='test6/one/two', " \
        "stage_test='GAMMA"

rx = re.compile(r'''world_wide='([^/]+)''')
parts = rx.findall(value)
print(parts)

This yields a list containing

['test1', 'test2', 'test3', 'test4', 'test5', 'test6']

See a demo on regex101.com.

Sign up to request clarification or add additional context in comments.

Comments

0

The regex you're looking for is probably as simple as pattern = r"world_wide='(.*)\/one". Here's a demo: https://regexr.com/3qffn

>>> import re
>>> value = ("world_wide='test1/one/two',stage_test='ALPHA')>,")
>>> pattern = r"world_wide='(.*)\/one"
>>> re.finall(pattern, value)
['test1']

What's making your question particularly hard to answer is that I think you have some typos in your example. On line 6 where you have stage_test='GAMMA')>]", I think you actually mean just stage_test='GAMMA')>,. Is that right?

Comments

0

Once I've to deal with regex, I always use https://regex101.com which is pretty nice and easy for fast prototyping and multiple case testing. For your regex please see the following link https://regex101.com/r/TLXDXl/1. My suggested pattern is:

r'world_wide='(?Ptest\d+)/'

which filters tries to match prefix using 'world_wide=', then matches the group named 'name', which starts with 'test' followed by one or more digit. more general approach would be to use '[\w\d]+' for name which will match any letter and digit.

Hope it helps

1 Comment

Thanks. Found regex101.com super useful.
0

Why don't you use just split() instead of re?:

for item in value.split(','):
    if 'world_wide' in item:
        world_parts = item.split('\'')
        test_parts = world_parts[1].split('/')

        if 'test' in test_parts[0]:
            print(test_parts[0])

1 Comment

Considering using this option. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.