Activerecord query with regex - ruby-on-rails

I need to find all the records which contains the given sub-string(art), and the condition is the given substring is either a complete word within the string or the start of any word within the string.
Sample data. Program
id | name
-----------
10 | Report of Quarter 1
11 | The Art Program
12 | The Artificial Program
From the above data, I must be able to get the record numbers 11 and 12 but not 10.
I am trying like this
Program.where("name ~* ?",'art\b')
but it's not working
I have also tried with
Program.where("regexp_match(name, ?)",'/art\b/i')
this too is not working
any help is really appreciable. Thanks!

EDITED
I guess you are using Postgres since you gave an example with regexp_match
Try Program.where("name ~* ?",'\mart') :)
You can see hidden in the Postgres docs ("Regular Expression Escapes" section) that \b means "backspace, as in C" whereas \m "matches only at the beginning of a word"

Related

Unsure how to NOT include a match in REGEXREPLACE

How do I turn Bay 13 | Brand PS/CPP into 13 using REGEXREPLACE in Google Sheets?
I can't figure out how to NOT include a pattern, as it seems whatever I type as the pattern it takes it out completely. I'm not used to this behavior in REGEX.
For instance:
REGEXREPLACE("(?i)bay ", )
returns 13 | Brand PS/CPP.
I want to keep the 13 (which could be any number between 1-35 including halves (12.5)), and get rid of everything after it including the "|" pipe.
try "everything after bay and before a pipe":
=REGEXEXTRACT(A1, "(?i)bay (.+) \|")
or if you want specifics:
=REGEXEXTRACT(A1, "(?i)bay (\d+(?:\.\d+)?) \|")
I think you want to use REGEXEXTRACT here rather than REGEXREPLACE:
REGEXEXTRACT("\d+(?:\.\d+)?", "Bay 13 | Brand PS/CPP")

Does anyone know how to fix this grep command

For 1, I can get 101 to 191 to print. How do I include 203 and up as well so that it includes everything from 10 up? For 2, I can get the first set of names starting with an L to print but not the ones in the and 230. Please don't suggest I use something else like awk or sed, I want to know how to do it the way I am currently trying to do it. How can I expand the ranges I am searching in order to include more. Thanks.
For 1) since it has to be 10 or more, it needs 2 or more digits, so just use this:
grep 'per[0-9]\{2,\}'
For 2), just do
grep 'per[0-9]*:L'
and, of course, you can combine them with
grep 'per[0-9]\{2,\}:L'
Try using the * to grep for repeated Numbers like: grep "per[0-9]*:L" idfile.txt
This is a more detailed answer :)
Regex - Matching arbitrary amount of numbers

How do you define the length of a parameter in ESC/POS?

I need to be able to print Hebrew characters on my Epson TM-T20ii. I am trying to get my printer to switch to character code page 36(PC862) using
ESC t36
for some reason the printer is switching to code page 3 and then printing the number 6.
Is there a way to let the printer know that the 6 is part of my command?
If you know of a different workaround please comment below.
Thanks
You are making a mistake, you aren't meant to replace n with an actual number.
The proper syntax in your case would be ←t$
Explanation: the manual says "ESC t n", n referring to the page sheet, however you don't replace n with a number rather with the ASCII character n, so in your example 36 = $ because $ is the 36th character on the ASCII table.

matching an address with regex doesn't match the target part

I'm not quite good in regex.
With my input string LT 1 BLK 4 LAKES OF PARKWAY 5 R/P & AMEND
I'd like to match just the only part between the figure 4 and 5 in the string.
meaning that, my expected result is LAKES OF PARKWAY.
I've tried to come up with a pattern to get such result.
\d+\s+([A-z ]+)(\d+.*?)*$
but with my pattern, it only matches BLK and 5 R/P & AMEND, as group #1 and group #2 respectively. At the end of my thought pattern, I decide to use end of string matching, $.
So, when 5 R/P & AMEND got matched, the pointer should move further behind to the sub sequence part. Then, ([A-z ]+) should match LAKES OF PARKWAY.
What's wrong with my pattern? and how to get it to work?
Any advice would be very much appreciated.
Try \d+\s+(\D+)\d+\D*$
\D means 'anything that is not \d, so it won't be allowed to match, for example, between the first 1 and 4, because then the ending of the regex would be rejected at the later 5.

parsing a text file where each record spans more than 1 line

I need to parse a text file that contains hundreds of records that span more than 1 line each. I'm new to Python and have been trying to do this with grep and awk in several complex ways but no luck yet.
The file contains records that look like this:
409547095517 911033 00:47:41 C44 00:47:46 D44 00:47:53 00:47:55
(555) 555-1212 00:47 10/31 100 Main Street - NW
Some_City TX 323 WRLS METRO PCS
P# 122-5217 ALT# 555-555-1212 LEC:MPCSI WIRELESS CALL Q
UERY CALLER FOR LOCATION QUERY CALLER FOR PHONE #*
Really I can do all I need to if I could just get these multi-line records condensed to 1 line per record. Each record will always begin with "40" or I could let 9110 indicate start as these will always be there and are unqiue providing 40 is at begining of line. I used a HEX editer and found that I could remove all line feeds (hex 0D0A) but this is not better than manually editing the files and programaticaly I'd need to not remove the last one per record. Some records will be only 2 lines but most will be 5 like this one.
Is there a way python or otherwise to concatonate the lines that make up a record into one line where 40 or maybe better choice where 9110 indicates the start of the record?
Any ideas or pointers will be much appreciated. I've got python and a good IDE and I'm good with grep and find but learning awk (don't laugh)...
awk will do it. You need to identify The line that starts a record. In this case it is 409547095517
So let's assume that to be safe if a line starts with 8 numbers it is the start of a record.
awk ' NR> 1 && /^[0-9]{8}/ { printf("\n") }
{printf("%s", $0) }
END{ printf("\n") }' filename > newfilename
Change the {8} to any number that works for you.

Resources