RegEx for N number of spaces in a string - regex

I am looking to create groups, that are separated by 4 spaces
The problem is that if the group contains any space, other than the 4 space separator, there is no match with the regex I have tried so far
This is what I have tried.
Let's say I have these 2 lines, with 4 spaces between the words
word 1 word 2
word1 word2
and the regex is
^([^ {4}]*) {4}([^ {4}]*)$
This matches only the 2nd line. The presence of any space anywhere other than the 4 space separator, will not match the line.
My expectation is to match and have the correct groups identified, in both these lines.

This RegEx might help you to divide your input strings into five groups, where the second and fourth groups are the four-space:
([a-zA-Z0-9_ ]*)(\s{4})([a-zA-Z0-9_ ]*)(\s{4})([a-zA-Z0-9_ ]*)
If you may not have space in your columns, you could simplify it using this RegEx:
(\w+)(\s{4})(\w+)(\s{4})(\w+)

After some experimentation and based on the good suggestions here, I came us with This RegEx:
^(.*?) (.*?) (.*?)$
On the surface it does what I need. The last line has more 4 space blocks at the end, but that should not happen. Any pitfall that I am not seeing?

Instead of using a non greedy dot star .*? approach, you could specify the characters that you want to match.
If your data contains for example only words, you could match 1+ word chars \w+ followed by a repeating pattern (\w+(?: \w+)*) to match a space and 1+ word chars followed by matching 4 spaces.
Note that if you want to match more that a word character, you could use a character class and add the characters that you would allow to match.
^(\w+(?: \w+)*) {4}(\w+(?: \w+)*) {4}(\w+(?: \w+)*)$
Regex demo

Related

Regular expression that matches at least 4 words starting with the same letter?

I've been trying to solve this problems for few hours but with no luck. The task is to write a regular expression that matches at least four words starting with the same letter. But! These words do not have to be one after another.
This regex should be able to match a line like this:
cat color coral chat
but also one like this:
cat take boom candle creepy drum cheek
Thank you!
So far I have got this regex but it only matches words when they are in order.
(\w)\w+\s+\1\w+\s+\1\w+\s+\1
If you have only words in the line that can be matched with \w:
\b(\w)\w*(?:(?:\s+\w+)*?\s+\1\w*){3}
Explanation
\b A word boundary to prevent a partial word match
(\w)\w* Capture a single word character in group 1 followed by matching optional word characters
(?: Non capture group to repeat as a whole part
(?:\s+\w+)*? Match 1+ whitespace chars and 1+ word chars in between in case the word does not start with the character captured in the back reference
\s+\1\w* Match 1+ whitespace chars, a backreference to the same captured character and optional word characters
){3} Close the non capture group and repeat 3 times
See a regex demo
Note that \s can also match a newline.
If the words that should with the same character should be at least 2 characters long (as (\w)\w+ matches 2 or more characters)
\b(\w)\w+(?:(?:\s+\w+)*?\s+\1\w+){3}
See another regex demo.
Another idea to match lines with at least 4 words starting with the same letter:
\b(\w)(?:.*?\b\1){3}
See this demo at regex101
This is not very accurate, it just checks if there are three \b word boundaries, each followed by \1 in the first group \b(\w) captured character to the right with .*? any characters in between.

Regex for finding words containing more then 3 'a' characters

I need to write a regex that will find all words with 3 or more 'a' letters. Suppose that each word is on a new line.
Example of correct words:
Anagram
Assassination
Abaca
I end up with something like this:
^([^aA]*a[^aA]*a[^aA]*a)$
But it will not work correctly if there will be more than 3 'a' letters or if word starts with 'a'.
I would keep it simple and just use:
\b\w*[Aa]\w*[Aa]\w*[Aa]\w*\b
Demo
This regex pattern matches any word containing three lower/upper a/A characters in it, appearing anywhere in the word.
Here is what I tried:
^(?i)(?:[b-z]*a){3}[a-z]*$
See an online demo
^ - Start line anchor.
(?i) - Match rest case-insensitive.
(?:[b-z]*a){3} - A non-capture group where you would match 0+ characters ranging from b-z upto a literal "a". Repeated three times.
[a-z]* - Match any possible remainder.
$- End line anchor.
If you want to use the anchors, you can add matching .* at the end, and add \n to the negated character class to prevent crossing newlines.
^[^aA\n]*[aA][^aA\n]*[aA][^aA\n]*[aA].*$
Regex demo
Or a bit shorter
^(?:[^aA\n]*[aA]){3}.*$
Regex demo

What is the Regex pattern "words with numbers in them" but not a number itself

How does a regex look like for
Input:
Rood Li-Ion 12 G6
Match:
"Rood" "Li-Ion" "G6"
1.
I tried
\b[\w-]+\b /g
But that matches the "12" also!
2.I tried
/([0-9]+)?[a-zA-ZĂȘ]/
But that didn't match G6.
I want all words even if they have a number in them but I dont want only numbers to match. How is this possible. Whitespace also shall not be part of the match.
"Rood Li-Ion 12 G6" shall become 3 strings of "Rood","Li-Ion","G6"
You can use
(?<!\S)(?!\d+(?!\S))\w+(?:-\w+)*(?!\S)
See the regex demo. It matches strings between whitespaces or start/end of string, and only when this non-whitespace chunk is not a digit only chunk.
Also, it won't match a streak of hyphens as your original regex.
Details
(?<!\S) - a left whitespace boundary
(?!\d+(?!\S)) - no one or more digits immediately to the right capped with whitespace or end of string is allowed
\w+(?:-\w+)* - one or more word chars followed with zero or more repetitions of - and one or more word chars
(?!\S) - a right whitespace boundary
This should suit your needs:
\b[\w-]*[a-zA-Z][\w-]*\b

Regex to match all groups between more than one space

I've got a string
198.21 543 G110P0GHTT SAW GHA + DBA 11998
And I'd like to match all groups of the string between spaces. So far I've come up with (?<=\s)(.*?)(?=\s) which matches all but the first group. Additionally, this does not count the GHA + DBA as a group. What can I add to this to ensure it includes the first record as well as anything MORE than one space
You don't need to use look arounds here. Just use this regex to match a non-whitespace string or substring separated by a single space:
\S+(?:\s\S+)*
RegEx Demo
RegEx Details:
\S+: Match 1+ non-space characters
(?:\s\S+)*: Match 0 or more non-space substring separated by a single space.

how to match a list of fixed length words separated by space or comma?

The words' length could be 2 or 6-10 and could be separated by space or comma. The word only include alphabet, not case sensitive.
Here is the groups of words that should be matched:
RE,re,rereRE
Not matching groups:
RE,rere,rel
RE,RERE
Here is the pattern that I have tried
((([a-zA-Z]{2})|([a-zA-Z]{6,10}))(,|\s+)?)
But unfortunately this pattern can match string like this: RE,RERE
Look like the word boundary has not been set.
You could match chars a-z either 2 or 6 - 10 times using an alternation
Then repeat that pattern 0+ times preceded by a comma or a space [ ,].
^(?:[A-Za-z]{6,10}|[A-Za-z]{2})(?:[, ](?:[A-Za-z]{6,10}|[A-Za-z]{2}))*$
Explanation
^ Start of string
(?:[A-Za-z]{6,10}|[A-Za-z]{2}) Match chars a-z 6 -10 or 2 times
(?: Non capturing group
[, ](?:[A-Za-z]{6,10}|[A-Za-z]{2}) Match comma or space and repeat previous pattern
)* Close non capturing group and repeat 0+ times
$ End of string
Regex demo
If lookarounds are supported, you might also assert what is directly on the left and on the right is not a non whitespace character \S.
(?<!\S)(?:[A-Za-z]{6,10}|[A-Za-z]{2})(?:[ ,](?:[A-Za-z]{6,10}|[A-Za-z]{2}))*(?!\S)
Regex demo
([a-zA-Z]{2}(,|\s)|[a-zA-Z]{6,10}|(,|\s))
This one will get only the words who have 2 letter, or between 6 and 10
\b,?([a-zA-Z]{6,10}|[a-zA-Z]{2}),?\b
You can use this
^(?!.*\b[a-z]{4}\b)(?:(?:[a-z]{2}|[a-z]{6,10})(?:,|[ ]+)?)+$
Regex Demo
This regex will match your first case, but neither of your two other cases:
^((([a-zA-Z]{2})|([a-zA-Z]{6,10}))(,|[ ]+|$))+$
I'm making the assumption here that each line should be a single match.
Here it is in action.