String Formatting in Python2.7 with result from Net commands - python-2.7

I created a batch file that writes user names to a file. It works perfectly and cleans up net user and writes the user names to a file so it would look like this:
Administrator Michael Guest
Pianoman Billy George
I don't know how many usernames there will be so my question is: how can I clean up this white space between the undetermined number of names since I don't know the length of names I'll be dealing with and thus not know how many spaces there will be.
My python program is supposed to read these names from a file and turn them into a list. I was planning on just using .split(" ") so ideally someone could suggest a way to get the difference down to one space between each name. I already looked at .format method, and it doesn't seem to be up to the task. I'm also open if there is a somewhat readable way (doubtable) to format this in batch.
BTW: I considered simply redirecting the output from dir /B C:\Users but this doesn't work in situation.

Use .split() without sep argument:
string.split(s[, sep[, maxsplit]])
Return a list of the words of the string s. If the optional second
argument sep is absent or None, the words are separated by
arbitrary strings of whitespace characters (space, tab, newline,
return, formfeed). If the second argument sep is present and not
None, it specifies a string to be used as the word separator. The
returned list will then have one more item than the number of
non-overlapping occurrences of the separator in the string. If
maxsplit is given, at most maxsplit number of splits occur, and
the remainder of the string is returned as the final element of the
list (thus, the list will have at most maxsplit+1 elements). If
maxsplit is not specified or -1, then there is no limit on the
number of splits (all possible splits are made).
The behavior of split on an empty string depends on the value of
sep. If sep is not specified, or specified as None, the result
will be an empty list. If sep is specified as any string, the result
will be a list containing one element which is an empty string.
Example:
>>> x='Administrator CLIENT1 Guest'
>>> x.split(' ')
['Administrator', '', '', '', '', '', '', '', '', '', '', '', 'CLIENT1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '','Guest']
>>> x.split()
['Administrator', 'CLIENT1', 'Guest']
>>>
Another approach:
>>> import string
>>> x='Administrator CLIENT1 Guest'
>>> string.split(x,' ')
['Administrator', '', '', '', '', '', '', '', '', '', '', '', 'CLIENT1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '','Guest']
>>> string.split(x)
['Administrator', 'CLIENT1', 'Guest']
>>>

Related

Split mathematical expression string with Regex in typescript

I have a mathematical expression (formula) in string format. What I'm trying to do is to split that string and make an array of all the operators and words collectively an array. I'm doing this by passing regex to split() function (As I'm new with regex I tried to create the regex to get my desired result). With this expression I'm getting an array seperated by operators, digits and words. But, somehow I'm getting an extra blank element in the array after each element. Have a look below to get what I'm exactly talking about.
My mathematical expression (formula):
1+2-(0.67*2)/2%2=O_AnnualSalary
Regex that I'm using to split it into an array:
this.createdFormula.split(/([?=+-*/%,()])/)
What I'm expecting an array should I get:
['1', '+', '2', '-', '(', '0', '.', '6', '7', '*', '2', ')', '/', '2', '%', '2', '=', 'O_AnnualSalary']
This what I'm getting:
['', '1', '', '+', '', '2', '', '-', '', '(', '', '0', '', '.', '', '6', '', '7', '', '*', '', '2', '', ')', '', '/', '', '2', '', '%', '', '2', '', '=', 'O_AnnualSalary']
So far what I've tried this expressions from many posts on SO:
this.createdFormula.split(/([?=+-\\*\\/%,()])/)
this.createdFormula.split(/([?=\\\W++-\\*\\/%,()])/)
this.createdFormula.split(/([?=//\s++-\\*\\/%,()])/)
this.createdFormula.split(/([?=+-\\*\\/%,()])(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)/)
this.createdFormula.split(/([?=+-\\*\\/%0-9,()])/)
Can anyone help me to fix this expression to get the desired result? If you need any more information please feel free to ask.
Any help is really appreciated.
Thanks
Assuming you have string match() available, we can use:
var input = "1+2-(0.67*2)/2%2=O_AnnualSalary,HwTotalDays";
var parts = input.match(/(?:[0-9.,%()=/*+-]|\w+)/g);
console.log(parts);

I am not getting expected result with my python code. Pl check

Expected Result = (444)333-4444', '444-555-3424'
Actual Result = [('(444)333-4444', '(444)', '', '333', '-', '4444', '', '', ''), ('444-555-3424', '444', '-', '555', '-', '3424', '', '', '')]
tell_op = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
(\d{3}) # first 3 digits
(\s|-|\.) # separator
(\d{4}) # last 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))? # extension
)''', re.VERBOSE)
oo = tell_op.findall('this is my phone number (444)333-4444, 444-555-3424')
print(oo)
You can make all the inner groups non-capturing groups:
tell_op = re.compile(r'''(
(?:\d{3}|\(\d{3}\))? # area code
(?:\s|-|\.)? # separator
(?:\d{3}) # first 3 digits
(?:\s|-|\.) # separator
(?:\d{4}) # last 4 digits
(?:\s*(?:ext|x|ext.)\s*(?:\d{2,5}))? # extension
)''', re.VERBOSE)
This will give you
['(444)333-4444', '444-555-3424']

python re.split() empty string

The following example is taken from the python re documents
re.split(r'\b', 'Words, words, words.')
['', 'Words', ', ', 'words', ', ', 'words', '.']
'\b' matches the empty string at the beginning or end of a word. Which means if you run this code it produces an error.
(jupyter notebook python 3.6)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-128-f4d2d57a2022> in <module>
1 reg = re.compile(r"\b")
----> 2 re.split(reg, "Words, word, word.")
/usr/lib/python3.6/re.py in split(pattern, string, maxsplit, flags)
210 and the remainder of the string is returned as the final element
211 of the list."""
--> 212 return _compile(pattern, flags).split(string, maxsplit)
213
214 def findall(pattern, string, flags=0):
ValueError: split() requires a non-empty pattern match.
Since \b only matches empty strings, split() does not get its requirement "non-empty" pattern match. I have seen varying questions related to split() and empty strings. Some I could see how you may want to do it in practice, example, the question here. Answers vary from "just can't do it" to (older ones) "it's a bug".
My question is this:
Since this is still an example on the python web page, should this be possible? is it something that is possible in the bleeding edge release?
The question in the in the link above involved
re.split(r'(?<!foo)(?=bar)', 'foobarbarbazbar'), it was asked in 2015 and there was no way to accomplish the requirements with just re.split(), is this still the case?
In Python 3.7 re, you can split with zero-length matches:
Changed in version 3.7: Added support of splitting on a pattern that could match an empty string.
Also, note that
Empty matches for the pattern split the string only when not adjacent to a previous empty match.
>>> re.split(r'\b', 'Words, words, words.')
['', 'Words', ', ', 'words', ', ', 'words', '.']
>>> re.split(r'\W*', '...words...')
['', '', 'w', 'o', 'r', 'd', 's', '', '']
>>> re.split(r'(\W*)', '...words...')
['', '...', '', '', 'w', '', 'o', '', 'r', '', 'd', '', 's', '...', '', '', '']
Also, with
re.split(r'(?<!foo)(?=bar)', 'foobarbarbazbar')
I get ['foobar', 'barbaz', 'bar'] result in Python 3.7.

javascript made '', after split regex

Hello i'm trying regex string.
Just i want remove number. I have this string
'dfmaks1,4412klaikd33,442'
var re = new RegExp("[0-9\,]");
var test = 'dfmaks1,4412klaikd33,442';
console.log(test.split(re));
Up in code result is
[ 'dfmaks', '', '', '', '', '', 'klaikd', '', '', '', '', '', '' ]
Why make '',s?
enter image description here
You may "invert" your regex to match any char other than digit/comma (i.e. this negated character class - [^0-9,]), 1 or more repetitions (add + after the character class), using
var test = 'dfmaks1,4412klaikd33,442';
console.log(test.match(/[^0-9,]+/g));
The String#match, when used with a regex with a global modifier (/g), will yield an array of all found non-overlapping match values.

Regex: Remove the complete line when it contains a word

I want to remove from my Database some Lines with Notepad++ or GrepWin. Any Line that contains MODsid must be deleted.
How could I do that?
The Lines
(10751, 1, '24555', '', 'product_info.php?products_id=206&MODsid=g8r9b625gev0eld02uaq5o2h74', '', '', '2013-07-05 10:31:28', 0),
(10752, 1, '69686', '', 'product_info.php?products_id=206&MODsid=geu3auqtc6gakfkh2lkkj18jn1', '', '', '2013-07-05 10:37:16', 0),
(10753, 1, '87957', '', 'product_info.php?products_id=206&MODsid=h3l5qr75ho7c8qn4uiqe3l5557', '', '', '2013-07-05 10:43:04', 0);
I don't know the Notepad++ RegEx syntax, but in general:
Replace \n.*MODsid.*\n with \n