string padded with optional blank with max length - regex

I have a problem building a regex. this is a sample of the text:
text 123 12345 abc 12 def 67 i 89 o 0 t 2
The numbers are sometimes padded with blanks to the max length (3).
e.g.:
"1" can be "1" or "1 "
"13" can be "13" or "13 "
My regex is at the moment this:
\b([\d](\s*)){1,3}\b
The results of this regex are the following: (. = blank for better visibility)
123.
12....
67.
89.
0....
2
But I need this: (. = blank for better visibility)
123
12.
67.
89.
0..
2
How can I tell the regex engine to count the blanks into the {1,3} option?

Try this:
\b(?:\d[\d\s]{0,2})(?:(?<=\s)|\b)
This will also cover strings like text 123 1 23 12345 123abc 12 def 67 i 89 o 0 t 2 and results in:
123
1.
23.
12.
67.
89.
0..
2

Does this do what you want?
\b(\d){1,3}\s*\b
This will also include whitespace (if available) after the selection.

I think you want this
\b(?:\d[\d\s]{0,2})(?!\d)
See it here on Regexr
the word boundary will not work at the end, because if the end of the match is a whitespace, there is no word boundary. Therefor I use a negative lookahead (?!\d) to ensure that there is no digit following.
But if you have a string like this "1 23". It will match only the "2" and the "23", but not the whitespace after the first "2".

Assuming you want to use the padded numbers somewhere else, break the problem apart into two; (simple) parsing the numbers, and (simple) formatting the numbers (including padding).
while ( $text =~ /\b(\d{1,3})\b/g ) {
printf( "%-3d\n", $1 );
}
Alternatively:
#padded_numbers = map { sprintf( "%-3d", $_ ) } ( $text =~ /\b(\d{1,3})\b/g )

Related

limit string in preg_match on php

Thanks for visit or just see my question.
I try to catch some pattern in string on php.
pattern :
min char = 100
max char = 100
contains [a-z] and [A-Z] and [0-9] and "#" and "-" and "=" and "+"
It's return TRUE if all on pattern match and founds in string.
else it will return FALSE.
$string = "jhJH#KJNkj-HJV=+0ksdbscasdJNKajcajnacakjBKBKjbidcubiISABUIhsdbchdsiweucIBHbhbHBUJHBjhJHBIYBHBCwJHBdcd";
if (preg_match("/^.*(?=.{100,100})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#])(?=.*[-])(?=.*[=])(?=.*[+]).*$/", $string)){
$result1 = true;
} else {
$result1 = false;
}
This works if the 100 char, But this is return TRUE too if the string length more than 100 digits.
I want return TRUE if string length not below 100 digits and not more than 100 digits and it should be contains a pattern.
Maybe any suggestions, in the pattern?
Thanks in advances
You failed to actually set the min and max length as you allow 0+ any chars other than a newline before actually requiring 100 chars in the string. You need to use
'/^(?=\D*\d)(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=[^#]*#)(?=[^-]*-)(?=[^=]*=)(?=[^+]*[+]).{0,100}$/s'
^^^^^^^
Here, the pattern will match 0 to 100 any chars incl. a newline (as /s allows . to match a newline) and requiring at least 1 digit, 1 lowercase, 1 uppercase, 1 #, 1-, 1 = and one +.
Actually, there are 7 conditions with specific requirements, so in fact, the minimum will be 7 chars. You need to adjust this pattern to what you really need.

R regular expression issue

I have a dataframe column including pages paths :
pagePath
/text/other_text/123-some_other_txet-4571/text.html
/text/other_text/another_txet/15-some_other_txet.html
/text/other_text/25189-some_other_txet/45112-text.html
/text/other_text/text/text/5418874-some_other_txet.html
/text/other_text/text/text/some_other_txet-4157/text.html
What I want to do is to extract the first number after a /, for example 123 from each row.
To solve this problem, I tried the following :
num = gsub("\\D"," ", mydata$pagePath) /*to delete all characters other than digits */
num1 = gsub("\\s+"," ",num) /*to let only one space between numbers*/
num2 = gsub("^\\s","",num1) /*to delete the first space in my string*/
my_number = gsub( " .*$", "", num2 ) /*to select the first number on my string*/
I thought that what's that I wanted, but I had some troubles, especially with rows like the last row in the example : /text/other_text/text/text/some_other_txet-4157/text.html
So, what I really want is to extract the first number after a /.
Any help would be very welcome.
You can use the following regex with gsub:
"^(?:.*?/(\\d+))?.*$"
And replace with "\\1". See the regex demo.
Code:
> s <- c("/text/other_text/123-some_other_txet-4571/text.html", "/text/other_text/another_txet/15-some_other_txet.html", "/text/other_text/25189-some_other_txet/45112-text.html", "/text/other_text/text/text/5418874-some_other_txet.html", "/text/other_text/text/text/some_other_txet-4157/text.html")
> gsub("^(?:.*?/(\\d+))?.*$", "\\1", s, perl=T)
[1] "123" "15" "25189" "5418874" ""
The regex will match optionally (with a (?:.*?/(\\d+))? subpattern) a part of string from the beginning till the first / (with .*?/) followed with 1 or more digits (capturing the digits into Group 1, with (\\d+)) and then the rest of the string up to its end (with .*$).
NOTE that perl=T is required.
with stringr str_extract, your code and pattern can be shortened to:
> str_extract(s, "(?<=/)\\d+")
[1] "123" "15" "25189" "5418874" NA
>
The str_extract will extract the first 1 or more digits if they are preceded with a / (the / itself is not returned as part of the match since it is a lookbehind subpattern, a zero width assertion, that does not put the matched text into the result).
Try this
\/(\d+).*
Demo
Output:
MATCH 1
1. [26-29] `123`
MATCH 2
1. [91-93] `15`
MATCH 3
1. [132-137] `25189`
MATCH 4
1. [197-204] `5418874`

How to print a Perl character class?

I was in a code review this morning and came across a bit of code that was wrong, but I couldn't tell why.
$line =~ /^[1-C]/;
This line was suppose to evaluate to a hex character between 1 and C, but I assume this line does not do that. The question is not what does match, but what does this match? Can I print out all characters in a character class? Something like below?
say join(', ', [1-C]);
Alas,
# Examples:
say join(', ', 1..9);
say join(', ', 'A'..'C');
say join(', ', 1..'C');
# Output
Argument "C" isn't numeric in range (or flop) at X:\developers\PERL\Test.pl line 33.
1, 2, 3, 4, 5, 6, 7, 8, 9
A, B, C
It matches every code point from U+0030 ("1") to U+0043 ("C").
The simple answer is to use
map chr, ord("1")..ord("C")
instead of
"1".."C"
as you can see in the following demonstration:
$ perl -Mcharnames=:full -E'
say sprintf " %s U+%05X %s", chr($_), $_, charnames::viacode($_)
for ord("1")..ord("C");
'
1 U+00031 DIGIT ONE
2 U+00032 DIGIT TWO
3 U+00033 DIGIT THREE
4 U+00034 DIGIT FOUR
5 U+00035 DIGIT FIVE
6 U+00036 DIGIT SIX
7 U+00037 DIGIT SEVEN
8 U+00038 DIGIT EIGHT
9 U+00039 DIGIT NINE
: U+0003A COLON
; U+0003B SEMICOLON
< U+0003C LESS-THAN SIGN
= U+0003D EQUALS SIGN
> U+0003E GREATER-THAN SIGN
? U+0003F QUESTION MARK
# U+00040 COMMERCIAL AT
A U+00041 LATIN CAPITAL LETTER A
B U+00042 LATIN CAPITAL LETTER B
C U+00043 LATIN CAPITAL LETTER C
If you have Unicode::Tussle installed, you can get the same output from the following shell command:
unichars -au '[1-C]'
You might be interested in wasting time browsing the Unicode code charts. (This particular range is covered by "Basic Latin (ASCII)".)
This is a simple program to test the range of that regexpr:
use strict;
use warnings;
use Test::More qw(no_plan);
for(my $i=ord('1'); $i<=ord('C'); $i++ ) {
my $char = chr($i);
ok $char =~ /^[1-C]/, "match: $char";
}
Generate this result:
ok 1 - match: 1
ok 2 - match: 2
ok 3 - match: 3
ok 4 - match: 4
ok 5 - match: 5
ok 6 - match: 6
ok 7 - match: 7
ok 8 - match: 8
ok 9 - match: 9
ok 10 - match: :
ok 11 - match: ;
ok 12 - match: <
ok 13 - match: =
ok 14 - match: >
ok 15 - match: ?
ok 16 - match: #
ok 17 - match: A
ok 18 - match: B
ok 19 - match: C
1..19
[1-9A-C] is that match a hex number between 1 and C
[a char-an another char] match all the chars between the two chars in the Unicode table

Get groups with regex and OR

I have something like this
AD ABCDEFG HIJKLMN
AB HIJKLMN
AC DJKEJKW SJKLAJL JSHELSJ
Rule: Always 2 Chars Code (AB|AC|AD) at line beginning then any number of 7 Chars codes following.
With this regex:
^(AB|AC|AD)|((\S{7})?
in this groovy code sample:
def m= Pattern.compile(/^(AB|AC|AD)|((\S{7})?)/).matcher("AC DJKEJKW SJKLAJL JSHELSJ")
println m.getCount()
I always get 8 as count, means it counts the spaces.
How do I get 4 groups (as expected) without spaces ?
Thanks from a not-yet-regex-expert
Sven
Using this code:
def input = [ 'AD ABCDEFG HIJKLMN', 'AB HIJKLMN', 'AC DJKEJKW SJKLAJL JSHELSJ' ]
def regexp = /^(AB|AC|AD)|((\S{7})+)/
def result = input.collect {
matcher = ( it =~ regexp )
println "got $matcher.count for $it"
matcher.collect { it[0] }
}
println result
I get the output
got 3 for AD ABCDEFG HIJKLMN
got 2 for AB HIJKLMN
got 4 for AC DJKEJKW SJKLAJL JSHELSJ
[[AD, ABCDEFG, HIJKLMN], [AB, HIJKLMN], [AC, DJKEJKW, SJKLAJL, JSHELSJ]]
Is this more what you wanted?
This pattern will match your requirements
^A[BCD](?:\s\S{7})+
See it here online on Regexr
Meaning start with A then either a B or a C or a D. This is followed by at least one group consisting of a whitespace followed by 7 non whitespaces.

Regex: mask all but the last 5 digits, ignoring non-digits

I want to match a number containing 17-23 digits interspersed with spaces or hyphens, then replace all but the last five digits with asterisks. I can match with the following regex:
((?:(?:\d)([\s-]*)){12,18})(\d[\s-]*){5}
My problem is that I can't get the regex to group all instances of [\s-] in the first section, and I have no idea how to get it to replace the initial 12-18 digits with asterisks (*).
How about this:
s/\d(?=(?:[ -]*\d){5,22}(?![ -]*\d))/*/g
The positive lookahead insures that there are at least 5 digits ahead of the just-matched digit, while the embedded negative lookahead insures that aren't more than 22.
However, there could still be more digits before the first-matched digit. That is, if there are 24 or more digits, this regex only operates on the last 23 of them. I don't know if that's a problem for you.
Even assuming that this is feasible with regex alone I'd bet that it would be way slower than using the non-capturing version of your regex and then reverse iterating over the match, leaving the first 5 digits alone and replacing the rest of them with '*'.
I think your regex is ok, but you might need to have a callback where you can insert the asterisks with another inline regex. The below is a Perl example.
s/((?:\d[\s-]*){12,18})((?:\d[\s-]*){4}\d)/ add_asterisks($1,$2) /xeg
use strict;
use warnings;
my $str = 'sequence of digits 01-2 3-456-7-190 123-416 78 ';
if ($str =~ s/((?:\d[\s-]*){12,18})((?:\d[\s-]*){4}\d)/ add_asterisks($1,$2) /xeg )
{
print "New string: '$str'\n";
}
sub add_asterisks {
my ($pre,$post) = #_;
$pre =~ s/\d/*/g;
return $pre . $post;
}
__END__
Output
New string: 'sequence of digits **-* *-***-*-*** ***-416 78 '
To give a java regex variant to Alan Moore's answer and using all word characters [a-zA-Z0-9] as \w instead of just digits \d.
This will also work with any length string.
public String maskNumber(String number){
String regex = "\\w(?=(?:\\W*\\w){4,}(?!\\W*\\w))";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(number);
while(m.find()){
number = number.replaceFirst(m.group(),"*");
}
return number;
}
This example
String[] numbers = {
"F4546-6565-55654-5457",
"F4546-6565-55654-54-D57",
"F4546-6565-55654-54-D;5.7",
"F4546-6565-55654-54-g5.37",
"hd6g83g.duj7*ndjd.(njdhg75){7dh i8}",
"####.####.####.675D-45",
"****.****.****.675D-45",
"**",
"12"
};
for (String number : numbers){
System.out.println(maskNumber(number));
}
Gives:
*****-****-*****-5457
*****-****-*****-*4-D57
*****-****-*****-*4-D;5.7
*****-****-*****-**-g5.37
*******.*********.(*******){*dh i8}
####.####.####.**5D-45
****.****.****.**5D-45
**
12