Regex - cut text between first and second slash

I almost have this regular expression, but I have problems with a leading slash - can anyone see what I'm wrong with? I just want to extract the first line, "projects" in this example here -> http://regexr.com?300av

+4
source share
5 answers

The easiest way is to split the string using a slash

var firstString = url.split('/')[1]; 

and you will have the first line, but if you want to extract using regext, than this will do it, just remember that do not add a global parameter to your regular expression.

 \/([a-zA-Z0-9]{0,}) 

I hope this helps

+23
source

It seems you can get your test using split, but for use using pure regex:

 s = '/projects/personal/29/56'; arr = s.match(/^\/([^/]*)\//); // arr[1] becomes 'project' document.writeln('<pre>Matched: [' + arr[1] + "]</pre>"); 
+2
source

I played with the answer from anubhava and got the following

 string expression returns /projects/personal/29/56 ([a-zA-Z])([^/]*)\/ projects/ /projects/personal/29/56 ([a-zA-Z])([^/]*) projects /projects123/personal/29/56 ([a-zA-Z])*?([a-zA-Z][0-9])([^/]*) projects123 

The second line reaches what bsod99 set: remove the first slah / and extract the first projects line from /projects/personal/29/56 29/56

+2
source

Adding to someone who is looking for such an answer. You can try adding a global flag to get other values ​​and in addition to the first part of the URL / projects.

 /projects/personal/29/56 

You just need to traverse the array present at the end of [i]

 /\/([a-zA-Z0-9]{0,})/g[i] expression returns i=0 /projects i=1 /personal i=2 /29 i=3 /56 
+1
source

I am adding an answer here only because I wanted to add it to [SO]: Python: return a line between // regex , and this question was marked as a duplicate of this (!!! while I was editing! !!).

script.py:

 #!/usr/bin/env python3 import re def main(): group_name = "between_slashes" words = [ "en/lemon_peel/n/", "ca/llimona/n/", "/asd /", "/asd", "asdf/", "aa//vv", ] pat = re.compile("^[^/]*/(?P<{}>[^/]*)/.*$".format(group_name)) for idx, word in enumerate(words): match = pat.match(word) if match is not None: print("{}: \"{}\" - \"{}\"".format(idx, word, match.group(group_name))) else: print("{}: \"{}\"".format(idx, word)) if __name__ == "__main__": main() 

Notes :

  • The template seems complicated, but I will do my best to explain this:
    1. 1st character ( ^ ) marks the beginning of a line
    2. The following [] corresponds to a character class: the contents ( ^ / ) tells it that it matches any character except /
    3. Further * says that the previous group ( # 2. ) May occur 0 or more times.
    4. Then follows the symbol /, which is our 1st (initial) guard
    5. Parentheses () indicate a group match, which can later be referenced by its name (ween_slashes). For more details check [Python 3.Docs]: Regular expression syntax (search (?P<name>...) )
    6. The content in brackets (after > ) is what we are looking for (we already know that): 0 or no longer / chars
    7. Next / symbol is our 2nd (final) guard
    8. Then . * Says: any character, 0 or more times
    9. Finally, $ marks the end of the line
  • I allowed myself to add additional lines for the search, in addition to those given in the question, to illustrate some corner cases
  • Works with Python 3 and Python 2

Output :

 c:\Work\Dev\StackOverflow\q45985002>"c:\Install\x64\Python\Python\3.5\python.exe" script.py 0: "en/lemon_peel/n/" - "lemon_peel" 1: "ca/llimona/n/" - "llimona" 2: "/asd /" - "asd " 3: "/asd" 4: "asdf/" 5: "aa//vv" - "" 
+1
source

Source: https://habr.com/ru/post/1396206/


All Articles