Corresponding "/" character in string

This is my first post and I'm new to Python. I'm trying to get this to work.

string 1 = [1/0/1, 1/0/2]
string 2 = [1/1, 1/2]

Trying to check a string, if I see two /, then I just need to replace 0with 1so that it becomes 1/1/1and 1/1/2.

If I don’t have two /, then I need to add it together with 1and change it in the format 1/1/1and 1/1/2, so line 2 becomes[1/1/1,1/1/2]

The ultimate goal is to get all rows matching the pattern x / 1 / x. Thanks for all the input on this. I tried this and it seems to work

for a in Port:
   if re.search(r'././', a):
     z.append(a.replace('/0/','/1/') )
   else:
     t1= a.split('/')
     if len(t1)>1 :
      t2= t1[0] + "/1/" + t1[1]
      z.append(t2)

There are a few lines left to take care of some exceptions, but it seems to do the job.

+4
3

/ - \/

+3

, , , RegEx.

:

# The string to test:
sTest = '1/0/2'

# Test the string:
if(sTest.count('/') == 2):
    # There are two forward slashes in the string
    # If the middle number is a 0, we'll replace it with a one:
    sTest = sTest.replace('/0/', '/1/')
elif(sTest.count('/') == 1):
    # One forward slash in string
    # Insert a 1 between first portion and the last portion:
    sTest = sTest.replace('/', '/1/')
else:
    print('Error: Test string is of an unknown format.')
# End If

RegEx, : \d+/0/\d+ \d+/\d+(?!/) , . , , .replace() (, ) .

EDIT: :

1: \d+/0/\d+ " ( (1) ), , (0), , ( (1) ).

2: \d+/\d+(?!/) " ( (1) ), ( (1) ), , ." , RegEx.

, , , ^ $ , . lookahead ((?!/)). , : ^\d+/0/\d+$ ^\d+/\d+$.

+3

https://regex101.com/r/rE6oN2/1 Click code generator on the left side. You are getting:

import re
p = re.compile(ur'\d/1/\d')
test_str = u"1/1/2"

re.search(p, test_str)
0
source

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


All Articles