swift uitextview limit number of line breaks to two at the time - ios

I wonder if its possible to limit the number of line break that you can do in row?
I have set a limit to the number of characters that can be inserted but users can still insert text like:
Sometext
(line break)
(line break)
(line break)
(line break)
(line break)
(line break)
some more text...
Is it possible to only low two line breaks at the time? So that the above text ends up like:
Some text
(line break)
(line break)
some more text...
Since I cant edit the text backend I must do it in the fronend.
I want too clean out the text and limit the amount of line breaks in a row to two after the user has finished typing

As long as there are 3 newline characters in a row in the text in the UITextView, you can replace them with only 2 newline characters.
while str.contains("\n\n\n") {
str = str.replacingOccurrences(of: "\n\n\n", with: "\n\n")
}

Related

Want file in CSV. I need a Regex to change a space to a comma on each line EXCEPT the Description Field which should remain with a space

Goal:
I want a CSV file as my result. I want to change the space char to a comma on each line of data. BUT, I also need the data for the 3rd field (Description) to remain as is with original space chars. Each line of data is terminated with a newline char.
Flipping spaces to commas on every line is easy with regex. But how do 'bookend' the string of text which will then become the 3rd/Description field and preserve its spaces? Currently I manually change commas back to spaces just in that text string. Painful.
Example of Final result needed (including column names)
Transaction Date,Posting Date,Description,Reference Number,Account Number,Amount
12/23,12/24,GOOGLE*DOMAINS SUPPORT.GOOGLCA,7811,8550,12.00
My sample data:
12/23 12/24 GOOGLE*DOMAINS SUPPORT.GOOGLCA 7811 8550 12.00
01/02 01/04 CREPEVINE - OAKLAND OAKLAND 234567 CA 1087 8220 16.32
01/06 01/07 AB* ABEBOOKS.CO J6YDBX HTTPSWWW.ABEBWA 6289 85332 6.98
01/20 01/21 SQ *BAGEL STREET CAFE Oakland CA 2313 44444 24.43
A few of My Regex attempts
This cmd changes spaces to commas over all 5 lines by combining it with Join cmd. Easy.
And just fyi: "\n" would not work for some reason so I do the <Ctrl+Enter> keys to inject a newline char, ie the two lines. For now it orks fine.
=regexreplace(join("
",A1:A5)," ",",")
RESULT:
12/23,12/24,GOOGLE*DOMAINS,SUPPORT.GOOGLCA,7811,8550,12.00
...
01/02,01/04,CREPEVINE,-,OAKLAND,OAKLAND,CA,1087,8550,16.32
...
Here is my poor attemp to bookend the description field, then flip commas back to spaces, but no luck either.
=REGEXREPLACE(A1,"(,[A-Z]+[A-Z],)"," ")
How do I craft a regex to do this?
cheers,
Damon
Using Regex101 to reverse learn how you did it
Can you try:
=index(if(len(A:A),regexreplace(A:A,"(?U)(.*) (.*) (.*) (\d[^A-Za-z]*) (\d.*) (\d.*)","$1,$2,$3,$4,$5,$6"),))

extract lines from a list of text files

okay so I have a list of files and 3 lines containing a word I need to extract from each line
basically each file can be looked at like this:
random
random
random
random
LINE 1 TEXT RANDOM TEXT
random
LINE 2 TEXT RANDOM TEXT
random
random
LINE 3 TEXT RANDOM TEXT
and what I'm looking to get is a text file containing this (without the FILE * PART):
FILE1 - LINE 1 TEXT RANDOM TEXT | LINE 2 TEXT RANDOM TEXT | LINE 3 TEXT RANDOM TEXT
FILE2 - LINE 1 TEXT RANDOM TEXT | LINE 2 TEXT RANDOM TEXT | LINE 3 TEXT RANDOM TEXT
FILE3 - LINE 1 TEXT RANDOM TEXT | LINE 2 TEXT RANDOM TEXT | LINE 3 TEXT RANDOM TEXT
FILE4 - LINE 1 TEXT RANDOM TEXT | LINE 2 TEXT RANDOM TEXT | LINE 3 TEXT RANDOM TEXT
TEXT RANDOM TEXT is obviously a random text that I'm looking to find, any help would be appreciated I tried powerGREP but it doesn't have an option to retrieve only unique records from each file
(meaning, only 1 match per search term, I get
LINE 1
LINE 2
LINE 2
LINE 3
)
powerGREP, I tried getting the search terms but got instead of 3 unique lines per file I got some 3 unique lines and some 4, 5, 6 because there are sometimes multiple lines with 1 of the search terms

How to make a variable non delimited file to be a delimited one

Hello guys I want to convert my non delimited file into a delimited file
Example of the file is as follows.
Name. CIF Address line 1 State Phn Address line 2 Country Billing Address line 3
Alex. 44A. Biston NJ 25478163 4th,floor XY USA 55/2018 kenning
And so on all the data are in this format.
First three lines are metadata and then the data.
How can I make it delimited in proper format using logic.
There are two parts in the problem:
how to find the column widths
how to split each line into fields and output a new line with delimiters
I could not propose an automated solution for the first one, because (not knowing anything about the metadata format), there is no clear way to find where one column ends and the next one begins. Some of the column headings contain multiple space-separated words and space is also used as a separator between the headings (and apparently one cannot use the rule "more than one space means the end of a heading name" because there's only one space between "Address line 2" and "Country" - and they're clearly separate columns. Clearly, finding the correct column widths requires understanding English and this is not something that you can write a program for.
For the second problem, things are much easier - once you have the column positions. If you figure the column positions manually (or programmatically, if you know something about the metadata that I don't - and you have a simple method for finding what's a column heading), then a program written in AWK can do this, for example:
cols="8,15,32,40,53,66,83,105"
awk_prog='BEGIN {
nt=split(cols,tabs,",")
delim=","
ORS=""
}
{ o=1 ;
for (i in tabs) { t=tabs[i] ; f=substr($0,o,t-o); sub(" *$","",f) ; print f
delim ; o=t } ;
print substr($0, o) "\n"
}'
awk -v cols="$cols" "$awk_prog" input_file
NOTE that the above program does not deal correctly with the case when the separator character (e.g. ",") appears inside the data. If you decide to use this as-is, be sure to use a separator that is not present in the input data. It may be better to modify the code to escape any separator characters found in the input data (there are different ways to do this - depends on what you plan to feed the output file to).

Adding tabs to non delimited text file with empty and variable length columns

I have a non-delimited text file and want to parse it to add tabs at specific spots to delimit columns. The columns are sometimes empty or vary in length, which is why I need to add tabs to those specific spots. I had found the answer to this once a couple of years ago on the net using batch, but now can't find it or the code. I already have the following code to replace more than 2 spaces in the file, but this doesn't account for when the columns are empty.
gc $FileToOpen | % { $_ -replace ' +',"`t" } | set-content $FileToSave
So, I need to read each line, but be able to only read a portion (certain number of characters) of it and add the tabs after each portion to itself.
Here is a sample of the data file, the top row is the header and the data rows have no blank lines in between them:
MRUN Number Name X Exception Reason Data CDM# Quantity D.O.S
000000 00000000 Name W MODIFIER CANNOT BE FILED WITHOUT 08/13/2015 0000000 0 08/13/2015
000000 00000000 Name W MODIFIER CANNOT BE FILED WITHOUT 0000000 0 08/13/2015
The second data row is missing Data.
Using Ansgar's answer, my code that does find empty fields:
gc $FileToOpen |
? { $_ -match '^(.{8})(.{12})(.{20})(.{3})(.{34})(.{62})(.{10})(.{22})(.{10})$' } |
% { "{0}`t{1}`t{2}`t{3}`t{4}`t{5}`t{6}`t{7}`t{8}" -f $matches[1].Trim(), $matches[2].Trim(), $matches[3].Trim(), $matches[4].Trim(), $matches[5].Trim(), $matches[6].Trim(), $matches[7].Trim(), $matches[8].Trim(), $matches[9].Trim() } |
Set-Content $FileToSave
Thanks for your patience Ansgar, I know I tried it! I really do appreciate the help!
Since you seem to have an input file with fixed-width columns, you should probably use a regular expression for transforming the input into a tab-delimited format.
Assume the following input file:
A B C
foo 13 22
bar 4 17
baz 142 23
The file has 3 columns. The first column is 6 characters wide, the other two columns 4 characters each.
The transformation could be done with a regular expression like this:
Get-Content 'C:\path\to\input.txt' |
? { $_ -match '^(.{6})(.{4})(.{4})$' } |
% { "{0}`t{1}`t{2}" -f $matches[1].Trim(), $matches[2].Trim(), $matches[3].Trim() } |
Set-Content 'C:\path\to\output.txt'
The regular expression defines the columns by character count and captures them in groups (parentheses). The groups can then be accessed as the indexes 1 and above of the resulting $matches collection. Trimming removes the leading/trailing whitespace. The format operator (-f) then inserts the trimmed values into the tab-separated format string.
If the last column has a variable width (because its values are aligned to the left and don't have trailing spaces) you may need to change the regular expression to ^(.{6})(.{4})(.{,4})$ to take care of that. The quantifier {,4} (or {0,4}) means up to four times the preceding expression.

Dividing a line of text into elements using a common delimiter

I have a playlist text file. I'm trying to extract a list of the artists and their songs. There are 39 line items and they appear as:
Rush - Red Sector A
Blues Traveler - Hook
This is a unicode file.
I'm trying to use the '-' as the delimiter and split the lines there:
x = open(u'list.txt')
for line in x:
line = line.strip()
elements = line.split('-')
artist = elements[0]
song = elements[1]
I get a traceback:
Traceback (most recent call last):
File "playlist.py", line 34, in <module>
song = line[1]
IndexError: list index out of range
It appears the delimiter is not being recognized. If I comment out "song = elements[1]" and print artists, I get the full line of text, delimiter and all. I've seen similar questions, but I can't get enough insight from their solutions to make this work. Any help would be appreciated.
This is due to the delimiting character '–' you think it's "-" but it's actually a different character that just looks like the hyphen. This character is not in the ASCII table, so we have to tell python we will be using utf-8, which covers almost all characters we might be using.
#-*- coding: utf-8 -*-
x = open(u'songs.txt')
delimiter = '–'
for line in x:
line = line.strip()
elements = line.split(delimiter)
artist = elements[0]
song = elements[1]
print "{artist} {song}".format(artist=artist,song=song)
My previous answers did not address the root of the problem, but this has been a great learning experience for me as well.

Resources