0

I'm sorry I'm pretty noob with regex since long time.

How can I get from this kind of string /projects/myproject/, the 2 following elements (projects , myproject) ?

Output :

print group1 : 'projects'

print group2 : 'myproject'

Thanks

4
  • This might not be a job for regexes, but for existing tools in your language of choice. What language are you using? You probably don't want to use a regex, but rather an existing module that has already been written, tested, and debugged. If you're using PHP, you want the parse_url function. If you're using Perl, you want the URI module. If you're using Ruby, use the URI module. Commented Mar 6, 2013 at 5:10
  • I'm using Python, I've noticed it in my post title :) Commented Mar 6, 2013 at 5:24
  • I know, but I don't know what Python uses. Point is, there are existing written, tested and debugged tools out there for you. Commented Mar 6, 2013 at 5:45
  • Duplicate of stackoverflow.com/questions/7894384/… Commented Mar 6, 2013 at 13:46

1 Answer 1

1
my_str = "/projects/myproject/"
matches = re.findall("/(.+?)/(.+)/",my_str)
print matches  #prints ['projects','myproject']
Sign up to request clarification or add additional context in comments.

7 Comments

your regex is working, but this is what I get : [('projects', 'targetstuffscholarships/')] 1) The last "/" is still here 2) How can I get an array because it's actually a list in an array Thanks
matches is an array of strings that the regex matched. Not an array of arrays. And, can you show us the string you are matching against?
The string is : /projects/targetstuffscholarships/
I'm not getting an array of array but a list included in an array. The parenthesis in my output means that is actually a list
matches[0] is what you are after. You can access your matches like matches[0][0] and matches[0][1]
|

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.