Match EOL character? - regex

I'm trying to capture the commands from a string of RTTTL commands like this:
2a4, 2e, 2d#, 2b4, 2a4, 2c, 2d, 2a#4, 2e., e, 1f4, 1a4, 1d#, 2e., d, 2c., b4, 1a4, 1p, 2a4, 2e, 2d#, 2b4, 2a4, 2c, 2d, 2a#4, 2e., e, 1f4, 1a4, 1d#, 2e., d, 2c., b4, 1a4
The regex I'm using is (\S+),|$ with global and multiline on, as I read that $ matches EOL when multiline mode is on, however this does not happen, and thus I cannot capture the last command 1a4, which ends the line. All the other commands are captured from the group.
What's the regex I should be using to capture the last command?

Just add a lookahead or non-capturing group like below. And get the string you want from group index 1.
(\S+)(?:,|$)
DEMO
OR
(\S+)(?=,|$)
DEMO
You don't need to have a capturing group also when using lookahead.
\S+(?=,|$)
(?=,|$) Positive lookahead asserts that the match must be followed by a , or end of the line anchor. \S+ matches one or more non-space characters.

another solution
$a = " 2a4, 2e, 2d#, 2b4, 2a4, 2c, 2d, 2a#4, 2e., e, 1f4, 1a4, 1d#, 2e., d, 2c., b4, 1a4, 1p, 2a4, 2e, 2d#, 2b4, 2a4, 2c, 2d, 2a#4, 2e., e, 1f4, 1a4, 1d#, 2e., d, 2c., b4, 1a4";
$r=explode(",",preg_replace("/\\s+/","",$a));
var_dump($r);
output:
array (size=37)
0 => string '2a4' (length=3)
1 => string '2e' (length=2)
2 => string '2d#' (length=3)
3 => string '2b4' (length=3)
4 => string '2a4' (length=3)
5 => string '2c' (length=2)
6 => string '2d' (length=2)
7 => string '2a#4' (length=4)
8 => string '2e.' (length=3)
9 => string 'e' (length=1)
10 => string '1f4' (length=3)
11 => string '1a4' (length=3)
12 => string '1d#' (length=3)
13 => string '2e.' (length=3)
14 => string 'd' (length=1)
15 => string '2c.' (length=3)
16 => string 'b4' (length=2)
17 => string '1a4' (length=3)
18 => string '1p' (length=2)
19 => string '2a4' (length=3)
20 => string '2e' (length=2)
21 => string '2d#' (length=3)
22 => string '2b4' (length=3)
23 => string '2a4' (length=3)
24 => string '2c' (length=2)
25 => string '2d' (length=2)
26 => string '2a#4' (length=4)
27 => string '2e.' (length=3)
28 => string 'e' (length=1)
29 => string '1f4' (length=3)
30 => string '1a4' (length=3)
31 => string '1d#' (length=3)
32 => string '2e.' (length=3)
33 => string 'd' (length=1)
34 => string '2c.' (length=3)
35 => string 'b4' (length=2)
36 => string '1a4' (length=3)

Related

Regex to get total price with space as separator

I need to build a regex that would catch the total price, here some exemple:
Total: 145.01 $
Total: 1 145.01 $
Total: 00.01 $
Total: 12 345.01 $
It's need to get any price that follow 'Total: ', without the '$'.
That what I got so far : (?<=\bTotal:\s*)(\d+.\d+)
RegExr
I assume:
each string must begin 'Total: ' (three spaces), the prefix;
the last digit in the string must be followed by ' $' (one space), the suffix, which is at the end of the string;
the substring between the prefix and suffix must end '.dd', where 'd' presents any digit, the cents;
the substring between the prefix and cents must match one of the following patterns, where 'd' represents any digit: 'd', 'dd', 'ddd', 'd ddd', 'dd ddd', 'ddd ddd', 'd ddd ddd', 'dd ddd ddd', 'ddd ddd ddd', 'd ddd ddd ddd' and so on;
the return value is the substring between the prefix and suffix that meets the above requirements; and
spaces will be removed from the substring returned as a separate step at the end.
We can use the following regular expression.
r = /\ATotal: {3}(\d{1,3}(?: \d{3})*\.\d{2}) \$\z/
In Ruby (but if you don't know Ruby you'll get the idea):
arr = <<~_.split(/\n/)
Total: 145.01 $
Total: 1 145.01 $
Total: 00.01 $
Total: 12 345.01 $
Total: 1 241 345.01 $
Total: 1.00 $
Total: 1.00$
Total: 1.00 $x
My Total: 1.00 $
Total: 12 34.01 $
_
The following matches each string in the array arr and extracts the contents of capture group 1, which is shown on the right side of each line.
arr.each do |s|
puts "\"#{(s + '"[r,1]').ljust(30)}: #{s[r,1] || 'no match'}"
end
"Total: 145.01 $"[r,1] : 145.01
"Total: 1 145.01 $"[r,1] : 1 145.01
"Total: 00.01 $"[r,1] : 00.01
"Total: 12 345.01 $"[r,1] : 12 345.01
"Total: 1 241 345.01 $"[r,1] : 1 241 345.01
"Total: 1.00 $"[r,1] : no match
"Total: 1.00$"[r,1] : no match
"Total: 1.00 $x"[r,1] : no match
"My Total: 1.00 $"[r,1] : no match
"Total: 12 34.01 $"[r,1] : no match
The regular expression can be written in free-spacing mode to make it self-documenting.
r = /
\A # match the beginning of the string
Total:\ {3} # match 'Total:' followed by 3 digits
( # begin capture group 1
\d{1,3} # match 1, 2 or 3 digits
(?:\ \d{3}) # match a space followed by 3 digits
* # perform the previous match zero or more times
\.\d{2} # match a period followed by 2 digits
) # end capture group 1
\ \$ # match a space followed by a dollar sign
\z # match end of string
/x # free-spacing regex definition mode
The regex can be seen in action here.

dart regex remove space phone

I tried all this regex solution but no match REGEX Remove Space
I work with dart and flutter and I tried to capture only digit of this type of string :
case 1
aaaaaaaaa 06 12 34 56 78 aaaaaa
case 2
aaaaaaaa 0612345678 aaaaaa
case 3
aaaaaa +336 12 34 56 78 aaaaa
I search to have only 0612345678 with no space and no +33. Just 10 digit in se case of +33 I need to replace +33 by 0
currently I have this code \D*(\d+)\D*? who run with the case 2
You may match and capture an optional +33 and then a digit followed with spaces or digits, and then check if Group 1 matched and then build the result accordingly.
Here is an example solution (tested):
var strs = ['aaaaaaaaa 06 12 34 56 78 aaaaaa', 'aaaaaaaa 0612345678 aaaaaa', 'aaaaaa +336 12 34 56 78 aaaaa', 'more +33 6 12 34 56 78'];
for (int i = 0; i < strs.length; i++) {
var rx = new RegExp(r"(?:^|\D)(\+33)?\s*(\d[\d ]*)(?!\d)");
var match = rx.firstMatch(strs[i]);
var result = "";
if (match != null) {
if (match.group(1) != null) {
result = "0" + match.group(2).replaceAll(" ", "");
} else {
result = match.group(2).replaceAll(" ", "");
}
print(result);
}
}
Returns 3 0612345678 strings in the output.
The pattern is
(?:^|\D)(\+33)?\s*(\d[\d ]*)(?!\d)
See its demo here.
(?:^|\D) - start of string or any char other than a digit
(\+33)? - Group 1 that captures +33 1 or 0 times
\s* - any 0+ whitespaces
(\d[\d ]*) - Group 2: a digit followed with spaces or/and digits
(?!\d) - no digit immediately to the right is allowed.
Spaces are removed from Group 2 with a match.group(2).replaceAll(" ", "") since one can't match discontinuous strings within one match operation.

Norwegian characters in C++ strings

I'm working with strings in C++, and have a question about how norwegian characters are treated.
If I run the following code;
int main()
{
string norwegian = "BLÅBÆRSYLTETØY";
for (auto &c : norwegian)
cout << c << " => " << static_cast<int>(c) << endl;
return 0;
}
the output at cmd becomes:
B => 66
L => 76
┼ => -59
B => 66
ã => -58
R => 82
S => 83
Y => 89
L => 76
T => 84
E => 69
T => 84
Ï => -40
Y => 89
Notice that the three norwegian characters are not printed correctly, and that the ASCII value is negative.
Is there any way to treat the string so that it uses the correct charactermap?
EDIT
The solution is to change the codepage from ANSI to UTF-7, which can be done by adding this before the code that does stringhandling;
system("chcp 65000");

How to detect if a string have letters in the beginning using scala funcs or regex?

I have a string number that might and might not have 2 or more chars in the beginning of the number, and maybe some chars that are not letters or numbers.
If its two or more in the beginning so delete the first 2 and clean the string from chars others than letters or numberss.
I want to detect that either using scala funcs or regex and clean this string.
examples:
"ABC12345" (after function) => "C12345"
"AB12345" (after function) => "12345"
"A12345" (after function) => "A12345"
"ABC1 23 +.4 5" (after function) => "C12345"
Regex matching characters which you want to remove:
^[A-Z]{2}|[^A-Z0-9]
It matches either exactly two letters at the beginning of string or anything other than [A-Z0-9].
Usage in Scala:
scala> val regex = """^[A-Z]{2}|[^A-Z0-9]""".r
regex: scala.util.matching.Regex = ^[A-Z]{2}|[^A-Z0-9]
scala> val ss = List("ABC12345", "A12345", "ABC1 23 +.4 5")
ss: List[String] = List(ABC12345, A12345, ABC1 23 +.4 5)
scala> ss.map(s => regex.replaceAllIn(s, ""))
res0: List[String] = List(C12345, A12345, C12345)

How to concatenate fields in select statements using Doctrine

I'm wondering how to concatenate two fields in DQL select statement with some literal between.
I have this for now but no luck...
$qb
->select('season.id, concat(competition.name, '-',season.name) AS specs')
->leftJoin('season.competition', 'competition')
->where('season.name LIKE :q')
->setParameter('q', '%'.$q.'%')
->setMaxResults($p)
;
We cannot send three arguments here, but we can do it like this,
$em = \Zend_Registry::get('em');
$qb_1 = $em->createQueryBuilder();
$q_1 = $qb_1->select( "reprt_abs.id" )
->addSelect( "CONCAT( CONCAT(reporter.firstname, ' '), reporter.lastname)" )
->from( '\Entities\report_abuse', 'reprt_abs' )
->leftJoin( 'reprt_abs.User', 'reporter' )
->getQuery()->getResult();
This part is that what you want:
$qb_1->select( "reprt_abs.id" )
->addSelect( "CONCAT( CONCAT(reporter.firstname, ' '), reporter.lastname)" )
Following is the output at my side:
array (size=19)
0 =>
array (size=2)
'id' => int 1
1 => string 'Jaskaran Singh' (length=14)
1 =>
array (size=2)
'id' => int 9
1 => string 'Harsimer Kaur' (length=14)
2 =>
array (size=2)
'id' => int 12
1 => string 'Jaskaran Singh' (length=14)
3 =>
array (size=2)
'id' => int 16
1 => string 'Jaskaran Singh' (length=14)
4 =>
array (size=2)
'id' => int 19
1 => string 'Jaskaran Singh' (length=14)
5 =>
array (size=2)
'id' => int 4
1 => string 'shilpi jaiswal' (length=14)
Solution I use in Doctrine 2.4+ on MySQL database PDO Platform:
$concat = new Query\Expr\Func('CONCAT', $name[$k]);
$concat .= ' as ' . $k;
$concat = str_replace(',', ',\' \',', $concat);
$this->query->addSelect($concat);
So $name[$k] is an array of fields, as many as you wish. I then add some spacing between the fields with the str_replace. $k is the name of the concat field, so the result of $concat is
"CONCAT(p.email,' ', h.phoneNumber,' ', p.officialName) as details"