Add a tab to the beginning of a specific line

A place to try and solve your RegEx problems.
Post Reply
rodp
Posts: 9
Joined: Thu Feb 02, 2012 5:12 pm

Add a tab to the beginning of a specific line

Post by rodp »

Hi All,

First post here so please go easy(!). I have some text (see below) which I've nearly got to where I want using TextCrawler regex but I'm struggling with this little bit. I'd like to add a tab to the line that is non blank and does not start with a number. Numbers range from 1 to 100.

Below I'd like the lines 'Chocolate Biscuits Bars', 'Everyday Biscuits' etc. to have a tab put in place at the beginning but not the lines that start with a number. I got as far as trying (\d+.*END.*\r\n) with a \t$1 which (obviously) pulls the lines with numbers... but how do you do the opposite and exclude blank lines?

Many thanks in advance for your help

Rodp

===============
SNPSHT1


1 Total Biscuits STORES01 TOTAL MARKET END

2 Total Sweet CONSSEC Childrens Biscuits END

Chocolate Biscuit Bars
Everyday Biscuits
Everyday Treats
Healthier Biscuits
Seasonal Biscuits
Special Treats

3 Total Savoury CONSSEC Crackers & Crispbreads END

Savoury Biscuits

4 Healthier Biscuits CONSSEC Healthier Biscuits END

5 CBBs CONSSEC Chocolate Biscuit Bars END

6 EDT CONSSEC Everyday Treats END

7 EDB CONSSEC Everyday Biscuits END

8 Special Treats CONSSEC Special Treats END

9 Seasonal CONSSEC Seasonal Biscuits END

10 Childrens CONSSEC Childrens Biscuits END

11 C&C CONSSEC Crackers & Crispbreads END

12 Savoury Biscuits CONSSEC Savoury Biscuits END

13 UBUK UBC-TRAD T$ UBUK END

14 UBUK Sweet CONSSEC Childrens Biscuits And

Chocolate Biscuit Bars
Everyday Biscuits
Everyday Treats
Healthier Biscuits
Seasonal Biscuits
Special Treats



UBC-TRAD T$ UBUK END

15 UBUK Savoury CONSSEC Crackers & Crispbreads And

Savoury Biscuits

===============
rodp
Posts: 9
Joined: Thu Feb 02, 2012 5:12 pm

Re: Add a tab to the beginning of a specific line

Post by rodp »

Hi All,

Got a little further on this problem but left with one final issue. I used:

search: ^(?!\d.*( END| And).*$).*
replace: \t\t\t$&
Multi line anchor tick box ticked

This left the lines with numbers at the beginning alone
2 Total Sweet CONSSEC Childrens Biscuits END

and tabbed / indented the lines:

Chocolate Biscuit Bars
Everyday Biscuits
Everyday Treats

However it also tabbed blank lines and also lines like

UBC-TRAD T$ UBUK END

I need the blank lines to be ignored (the blank lines may contain tabs and spaces but no letters) and I need the above line (no beinning number but the word END at the end) to be tabbed differently to the others.

One other slight issue is that the result in textcrawler looks fine but when you look at it in notepad for example using the above formula, one or two of the lines get tabbed - bit wierd but perhaps I need to include some \r\n codes in the regex somewhere.

Please could someone help me amend the regex to try and do this?

Hope someone can help as I'm really srtruggling

Many thanks

Rodp
rodp
Posts: 9
Joined: Thu Feb 02, 2012 5:12 pm

Re: Add a tab to the beginning of a specific line

Post by rodp »

hi all (again),

progress has been made but in the opposite direction(!). I've tried selecting the lines I don't want to edit thinking that if I can get that to work then it might be easy enough to inverse it. Well here's my regex:

Search: ^.*?(\d{1,}.*( END| And))|^([\s\w\W])$|^(\s[\w\W])$
Replace: ==ttt==$& [just used this now so that you can see where the tabs are going to go in the end]
Multiline Anchors ticked

it gives this result which is exactly what I need of this stage of manipulation but I've now got to inverse it so that infact the lines that don't currently have ==ttt== at the beginning do, and that that currently do, don't.

Now...... can anyone help me with this little bit? I've been trying a combination of (^.... and ?!.... expression but it's not wanting to play ball.

Please help! :)

Thanks

Rodp

=================================

==ttt==13 UBUK UBC-TRAD T$ UBUK END
==ttt==
==ttt==14 UBUK Sweet CONSSEC Childrens Biscuits And
==ttt==
Chocolate Biscuit Bars
Everyday Biscuits
Everyday Treats
Healthier Biscuits
Seasonal Biscuits
Special Treats
==ttt==
==ttt==
==ttt==
UBC-TRAD T$ UBUK END
==ttt==
==ttt==15 UBUK Savoury CONSSEC Crackers & Crispbreads And
==ttt==
Savoury Biscuits
==ttt==
User avatar
Fool4UAnyway

Re: Add a tab to the beginning of a specific line

Post by Fool4UAnyway »

You could deal with this in two ways.

If you know how to change the opposite lines, you could change all lines and then unchange all the lines you didn't want to change.

I think that would have been easier than creating a regex I can't understand at first sight.
It's way too complex for just achieving what you do want.
Maybe if you understand what you created, you know more about regular expressions than I do.
I hope you enjoy(ed) the time spending to create that thing!

It seems you want to change lines on which the first non-white space character is not a digit.
You say there are lines with (only) white space, or perhaps there are lines that already start with white space. You may not want to change those. (I myself prefer to never have any lines containing _only_ white space characters.)

Try this regular expression in the Find field:
\r\n(\s*)([^\d\s])

Use either of the following expressions in the Replace By field, depending on whether you want the tab before or after any already existing white space characters:
\r\n\t$1$2
\r\n$1\t$2

If you do not want the white spaces to return, leave the $1.

If you have no white space at all on the start of those lines, you can use the following expressions.

Find:
\r\n([^\d\s])

Replace By:
\r\n\t$1

But if you are not sure, use the previous alternative: if there is any white space, you just do not re-place it.

\r\n I use this just to get the start to the start of a new line, except the first line. You may have to edit that manually. In version 1 of Text Crawler ^ could be used. I sometimes use that version to be able to use that feature.

\s means any white space character (space or tab)
* means any consecutive string of those, also allowing none

[^] means: NOT any of the characters following ^ between the square brackets
\d is any digit 0 - 9 which you apparently already know
I also exclude the white space characters explicitly here, \s* might not take 'm all if somehow your regular expressions search match engine is set to not be (too) greedy
You may want to _allow_ only alphabetical characters. Then you could use [a-z] or [a-zA-Z] in case of a case sensitive search. You can extend the group of [...] characters to match you desire.

Does this help you enough?
rodp
Posts: 9
Joined: Thu Feb 02, 2012 5:12 pm

Re: Add a tab to the beginning of a specific line

Post by rodp »

Hi Fool4UAnyway,

Thanks for replying. I think you're right in that I should stick with what I've got and split the process in two. Add a tag to the lines (that I don't want to change) firstly and then use reg ex again to swap it around. I was trying to be clever and just do it in one expression but it's proving difficult. Looking in notepad plus some lines only have a line free rather than character return too and this is complicating things and so will create a second step like you suggested.

One question though, I see you tend to represent the start of the line by \r\n. I always thought that represented the end of a line. Perhaps \r\n regex can be used when you don't use the Multiline anchor option in textcrawler? I've been using this option and the ^ $ to handle each line.

Thanks for the advise and if you've got any more ideas, let me know.

Cheers

Rodp
Post Reply