Tapatalk

Regex to find lines with x number of blanks

Regex to find lines with x number of blanks

24
Basic UserBasic User
24

    Jan 19, 2010#1

    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

    236
    MasterMaster
    236

      Jan 19, 2010#2

      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

      24
      Basic UserBasic User
      24

        Jan 23, 2010#3

        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

        236
        MasterMaster
        236

          Jan 23, 2010#4

          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.

          24
          Basic UserBasic User
          24

            Jan 24, 2010#5

            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

            236
            MasterMaster
            236

              Jan 24, 2010#6

              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.