Regex to find lines with x number of blanks

Find, replace, find in files, replace in files, regular expressions

Regex to find lines with x number of blanks

Postby dictdoc » Tue Jan 19, 2010 2:04 am

Hello,
I am a newbie to regex and would like to a regex which will enable me to find specific number of blank spaces on a line.
I have UTF data in Hindi which has 2-3-4-5-6 words on the same line separated by blanks e.g.
a b
a b c
a b c d
a b c d e
where a,b,c,d,e and so on stand for words.
I would like to write a regex which could help me find say lines having only one space or two spaces or three spaces and so on.
I have gone through the regex queries and find nothing which meets my need. Any help would be welcome.
I tried a regex like
Code: Select all
 . *

but I got weird results.
All help will be gratefully acknowledged,
Best regards,
Dictionary doctor
dictdoc
Basic User
Basic User
 
Posts: 18
Joined: Tue Jan 19, 2010 1:56 am

Re: Regex to find lines with x number of blanks

Postby pietzcker » Tue Jan 19, 2010 2:33 am

From the top of my head, I'd suggest the following Perl regex:

^([^\s]* ){5}[^\s]*$

^ matches the start of the line
([^\s]* ) matches zero or more non-space/non-tab/non-CRLF characters, followed by exactly one space character
{5} repeats this pattern 5 times - so if you want to match lines with exactly 3 spaces, write a 3 here instead of 5.
[^\s]* matches zero or more non-space/non-tab/non-CRLF characters
$ matches the end of the line

HTH,
Tim
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Regex to find lines with x number of blanks

Postby dictdoc » Fri Jan 22, 2010 10:04 pm

Hello,
This is in continuation of my earlier query re. regex where I wanted to know the answer to finding the occurrence of two regular expression types within a line.
I also have in the dictionary IPA (phonetic representations) represented by /IPA/.
How do I handle a slash in the perl regex given?
I have tried all tricks and nothing seems to work.
Best regards,
And many thanks in advance.

Dictionary Doctor
dictdoc
Basic User
Basic User
 
Posts: 18
Joined: Tue Jan 19, 2010 1:56 am

Re: Regex to find lines with x number of blanks

Postby pietzcker » Sat Jan 23, 2010 7:36 am

Not sure what you mean. In Perl, regex objects usually are delimited by slashes, like /.*/ - these slashes have to be dropped if you want to use the regex in UE. Otherwise, slashes are no special characters, so if you want to match one, just type one.
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Regex to find lines with x number of blanks

Postby dictdoc » Sun Jan 24, 2010 9:40 am

Sorry, guess I should have been a bit more clear.
The structure of the dictionary is as under:
Headword,/Phonetic representation, meanings and examples.
It so happens that at times two dictionary entries are on the same line as in the case below
Headword,/Phonetic representation/, meanings and examples Headword,/Phonetic representation, meanings and examples.
The only explicit method of finding the such instances is by trying to find all instances of / through a regex since checking the entries by hand would be very painful. Commas are more frequent and hence would not work.
This is why I requested a Regex to locate the occurence of more than one / which would efficiently allow me to identify such entries.
I'll try the suggestion you have provide. Many thanks for answering,

Best regards,

Dictionary Doctor
dictdoc
Basic User
Basic User
 
Posts: 18
Joined: Tue Jan 19, 2010 1:56 am

Re: Regex to find lines with x number of blanks

Postby pietzcker » Sun Jan 24, 2010 1:42 pm

If you want to find pairs of slashes and everything between them (as long as they are on the same line), I suggest you use /([^/,\r\n]*)/ as your regex. The content between the slashes will be in backreference \1 (that you can use in the replace box, for example).

Content between slashes can be anything except slashes (of course), commas, and newline characters.
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm


Return to Find/Replace/Regular Expressions