Reputation: 193
I need to write a regex that checks that in the string there is 12 | (pipes) and 13 other words any characters without | pipe ( number letters and special characters). the input for the regex is :
00000003 | 1 | 1 |0 | test name | [email protected] | Street | city | state | zip code | GBR | description | 30
I tried to build it like this:
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])+(\|{1})+
([A-Za-z0-9!"#$%&'()*+,./:;<=>?@\^_`{}~-])
but the regex does not check 12 pipes. in addition, I've tried to build this regex
1)
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])([\|])
(?:[^|])
2)
(?:[^|])\|
(?:[^|])\|
(?:[^|])\|
(?:[^|])\|
(?:[^|])\|
(?:[^|])\|
(?:[^|])\|
(?:[^|])\|
(?:[^|])\|
(?:[^|])\|
(?:[^|])\|
(?:[^|])
3) (/|{12})
none of them didn't work for me. any advice?
Upvotes: 0
Views: 100
Reputation: 20737
Something like this?
^([^|]+\|){12}[^|]+$
In English:
Find 12 instances of anything not a pipe followed by a pipe. Then make sure it ends in something which is not a pipe
https://regex101.com/r/R8TqaL/1
Upvotes: 0
Reputation: 21902
You should use this: ^(?:[^\|]+\|){12}[^\|]+$
So properly escaped as a Java String: String pattern = "^(?:[^\\|]+\\|){12}[^\\|]+$";
This reads like this:
^
: From the beginning of the expression
(?:[^\|]+\|)
: look for a bunch of non-| followed by a | . The (?: thing is for non-capturing groups. It tells the regex engine you don't actually care about the field value, just that it's there. It's an optimization.
{12}
: exactly 12 times
[^\|]+
: then one last 13th field of non-|
$
: And at that point you should reach the end of the expression
Actual example at: https://www.regexplanet.com/share/index.html?share=yyyydfv274d
I'm sure you already know, but for sake of completeness, here are all the regex tokens in Java and their meanings: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Upvotes: 1
Reputation: 1092
I've checked this one:
^.*?\|.*?\|.*?\|.*?\|.*?\|.*?\|.*?\|.*?\|.*?\|.*?\|.*?\|.*?\|.*
Upvotes: 0
Reputation:
This seems to fit
"^(?:[^|]*\\w[^|]*\\|){12}[^|]*\\w[^|]*$"
https://regex101.com/r/TwYdDV/1
Explained
^ # BOS
(?: # Cluster
[^|]* \w [^|]* # Exists a word char
\| # Pipe
){12} # End cluster, do 12 times
[^|]* \w [^|]* # Exists a word char
$ # EOS
Upvotes: 0