If I have a list with following elements
list[0] = "blach blah blah"
list[1] = "SELECT something"
list[2] = "some more text"
list[3] = "some more text"
How can I find the index of where the string starts with SELECT.
I can do list.indexOf("SELECT something");
But this is a dynamic list. SELECT something wont always be SELECT something. it could be SELECT somethingelse or anything but first word will always be SELECT.
Is there a way to apply regex to the indexOf search?
def list = ["blach blah blah", "SELECT something", "some more text", "some more text"]
def index = list.findIndexOf { it ==~ /SELECT \w+/ }
This will return the index of the first item that matches the regex /SELECT \w+/. If you want to obtain the indices of all matching items replace the second line with
def index = list.findIndexValues { it ==~ /SELECT \w+/ }
You can use a regex in find:
def list = ["blach blah blah", "SELECT something", "some more text", "some more text"]
def item = list.find { it ==~ /SELECT \w+/ }
assert item == "SELECT something"
list[1] = "SELECT somethingelse"
item = list.find { it ==~ /SELECT \w+/ }
assert item == "SELECT somethingelse"
Related
ViewModel
fun changeQty(textField: TextFieldValue) {
val temp1 = textField.text
Timber.d("textField: $temp1")
val temp2 = temp1.replace("[^\\d]".toRegex(), "")
Timber.d("temp2: $temp2")
_qty.value = textField.copy(temp2)
}
TextField
OutlinedTextField(
modifier = Modifier
.focusRequester(focusRequester = focusRequester)
.onFocusChanged {
if (it.isFocused) {
keyboardController?.show()
}
},
value = qty.copy(
text = qty.text.trim()
),
onValueChange = changeQty,
label = { Text(text = qtyHint) },
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(
onDone = {
save()
onDismiss()
}
)
)
Set KeyboardType.Number, it display 1,2,3,4,5,6,7,8,9 and , . - space.
I just want to get integer like -10 or 10 or 0.
But I type the , or . or -(not the front sign), it show as it is.
ex)
typing = -10---------
hope = -10
display = -10---------
I put regular expression in
val temp2 = temp1.replace("[^\\d]".toRegex(), "")
But, it doesn't seem to work.
How I can get only integer(also negative integer)?
Use this regex (?<=(\d|-))(\D+) to replace all non digit characters, except first -.
fun getIntegersFromString(input: String): String {
val pattern = Regex("(?<=(\\d|-))(\\D+)")
val formatted = pattern.replace(input, "")
return formatted
}
Check it here
I'm not really versatile in regex, especially multi-line so i hope someone can help me out here.
Based on the following example, I'm trying to find all the field definitions of type Code that don't have the "TableRelation"-property set.
so in this example, this would be the field "Holding Name"
table 123 "MyTable"
{
fields
{
field(1000; "Created on"; Date)
{
Caption = 'Created on';
DataClassification = CustomerContent;
Editable = false;
}
field(2000; "Created by"; Code[50])
{
Caption = 'Created by';
TableRelation = User."User Name";
DataClassification = CustomerContent;
Editable = false;
}
field(3000; Resigned; Boolean)
{
Caption = 'Resigned';
DataClassification = CustomerContent;
}
field(4000; "Holding No."; Code[20])
{
Caption = 'Holding No.';
TableRelation = Contact."No." where(Type = const(Company));
DataClassification = CustomerContent;
trigger OnValidate()
var
[...]
begin
[...]
end;
}
field(4010; "Holding Name"; Code[100])
{
Caption = 'Holding Name';
DataClassification = CustomerContent;
}
field(5000; "Geocoding Entry No."; Integer)
{
Caption = 'Geocoding Entry No.';
DataClassification = CustomerContent;
}
}
keys
{
key(AppliesToContact; "Holding No.", "Holding Name", "Company Level") { }
}
}
I Managed to match the fields of type "Code" properly... field\(\d+;.+; ?(?:C|c)ode\[\d+\]\)\n?\s*\{(?:\n|.)*?\}
But i don't know how to correctly exclude matches containing "TableRelation" at least this doesn't work the way I hoped. - I get one HUGE match with it :-(
field\(\d+;.+; ?(?:C|c)ode\[\d+\]\)\n?\s*\{((?!(T|t)able(R|r)elation)\n*.*)*?\}
p.s. if you're wondering: The sample I'm parsing is written in AL-Language, a proprietary language for MS Business central.
You can match field( and the digits between the square brackets before the closing parenthesis using a negated character class starting with [^
The same negated character class approach can also be taken for asserting not TableRelation between curly braces.
Not that you can write (?:C|c) as [Cc] using a character class instead of using an alternation |
Assuming the curly brace after field has a single non nested closing curly:
field\([^()]+; ?[Cc]ode\[\d+\]\)\s*{(?![^{}]*[Tt]able[Rr]elation)[^{}]*}
The pattern matches:
field\([^()]+ Match field( and 1+ chars other than ( ) (which can also match a newline)
; ?[Cc]ode Match ; optional space and Code/code
\[\d+\]\) Match [ 1+ digits ])
\s*{ Match optional whitespace chars (which can also match a newline) and {
(?![^{}]*[Tt]able[Rr]elation) Negative lookahead, assert not TableRelation after the openin curly
[^{}]* Match optional repetitions of any character except { }
} Match closing }
See a regex demo.
With a caseless research and with a regex engine that allows atomic groups and possessive quantifiers, you can write:
\bfield\((?>[^);]*;\s*)*code\b[^)]*\)\s*{(?>[^\w}]*+(?!tablerelation\s*=)\w+)*[^\w}]*}
demo
This pattern is based on negative character classes to stop the greedy quantifiers as in The four birds answer.
Atomic groups (?>...) and possessive quantifiers *+ are used to reduce the backtracking. In particular, the presence of tablerelation is only tested after a range of non-word characters with a negative lookahead.
Note that the code part can be everywhere between the parenthesis after field.
The following regex can capture the Code[...] value of areas not having 'TableRelation'.
/field\([^)]+; Code\[(\d+)\]\)\n\s+{((?!TableRelation).)+?}\n/gs
It uses g(global) and s(dotall) flags.
A notable part of this regexp is the ((?!TableRelation).)+? expression.
(?!TableRelation) : negative lookahead(should not appear)
((?!TableRelation).)+? : not having 'TableRelation', match as few as possible
I created a simple JS snippet. The code uses two steps to extract.
const regexp = /field\([^)]+; Code\[(\d+)\]\)\n\s+{((?!TableRelation).)+?}\n/gs;
const target = `
table 123 "MyTable"
{
fields
{
field(1000; "Created on"; Date)
{
Caption = 'Created on';
DataClassification = CustomerContent;
Editable = false;
}
field(2000; "Created by"; Code[50])
{
Caption = 'Created by';
TableRelation = User."User Name";
DataClassification = CustomerContent;
Editable = false;
}
field(3000; Resigned; Boolean)
{
Caption = 'Resigned';
DataClassification = CustomerContent;
}
field(4000; "Holding No."; Code[20])
{
Caption = 'Holding No.';
TableRelation = Contact."No." where(Type = const(Company));
DataClassification = CustomerContent;
trigger OnValidate()
var
[...]
begin
[...]
end;
}
field(4010; "Holding Name"; Code[100])
{
Caption = 'Holding Name';
DataClassification = CustomerContent;
}
field(4050; "Holding Name"; Code[80])
{
Caption = 'Holding Name 2';
DataClassification = CustomerContent;
}
field(5000; "Geocoding Entry No."; Integer)
{
Caption = 'Geocoding Entry No.';
DataClassification = CustomerContent;
}
}
keys
{
key(AppliesToContact; "Holding No.", "Holding Name", "Company Level") { }
}
}
`;
// step 1: extract field(...){...} chunks that do not contain "TableRelation"
const matchedBlocks = target.match(regexp);
// step 2: extract code values
const codes = matchedBlocks.map(m => m.match(/; Code\[(\d+)\]/)[1] );
console.log(codes);
it's my first post.
I work to Quasar (Vue.js)
I have list of jobs, and in this list, i have words with special caractere.
Ex :
[ ...{ "libelle": "Agent hôtelier" },{"libelle": "Agent spécialisé / Agente spécialisée des écoles maternelles -ASEM-"},{ "libelle": "Agriculteur / Agricultrice" },{ "libelle": "Aide aux personnes âgées" },{ "libelle": "Aide de cuisine" },...]
And on "input" i would like to search "Agent spécialisé" but i want to write "agent specialise" (without special caractere) or the initial name, i want to write both and autocomplete my "input".
I just don't fin the solution for add to my filter code ...
My input :
<q-select
filled
v-model="model"
use-input
hide-selected
fill-input
input-debounce="0"
:options="options"
hint="Votre métier"
style="width: 250px; padding-bottom: 32px"
#filter="filterFn"
>
</q-select>
</div>
My code :
export default {
props: ['data'],
data() {
return {
jobList: json,
model: '',
options: [],
stringOptions: []
}
},
methods: {
jsonJobsCall(e) {
this.stringOptions = []
json.forEach(res => {
this.stringOptions.push(res.libelle)
})
},
filterFn(val, update) {
if (val === '') {
update(() => {
this.jsonJobsCall(val)
this.options = this.stringOptions
})
return
}
update(() => {
const regex = /é/i
const needle = val.toLowerCase()
this.jsonJobsCall(val)
this.options = this.stringOptions.filter(
v => v.replace(regex, 'e').toLowerCase().indexOf(needle) > -1
)
})
},
}
}
To sum up : i need filter for write with or witouth special caractere in my input for found in my list the job which can contain a special character.
I hope i was clear, ask your questions if i haven't been.
Thanks you very much.
I am not sure if its work for you but you can use regex to create valid filter for your need. For example, when there is "e" letter you want to check "e" or "é" (If I understand correctly)
//Lets say we want to match "Agent spécialisé" with the given search text
let searchText = "Agent spe";
// Lets create a character map for matching characters
let characterMap = {
e: ['e', 'é'],
a: ['a', '#']
}
// Replacing special characters with a regex part which contains all equivelant characters
// !Remember replaceAll depricated
Object.keys(characterMap).forEach((key) => {
let replaceReg = new RegExp(`${key}`, "g")
searchText = searchText.replace(replaceReg, `[${characterMap[key].join("|")}]`);
})
// Here we create a regex to match
let reg = new RegExp(searchText + ".*")
console.log("Agent spécialisé".match(reg) != null);
Another approach could be the reverse of this. You can normalize "Agent spécialisé". (I mean replace all é with normal e with a regex like above) and store in the object along with the original text. But search on this normalized string instead of original.
In one of my mongodb collection, I have a date string that has a mm/dd/yyyy format. Now, I want to query the 'mm' string.
Example, 05/20/2016 and 04/05/2015.
I want to get the first 2 characters of the string and query '05'. With that, the result I will get should only be 05/20/2016.
How can I achieve this?
Thanks!
For a regex solution, the following will suffice
var search = "05",
rgx = new RegExp("^"+search); // equivalent to var rgx = /^05/;
db.collection.find({ "leave_start": rgx });
Testing
var leave_start = "05/06/2016",
test = leave_start.match(/^05/);
console.log(test); // ["05", index: 0, input: "05/06/2016"]
console.log(test[0]); // "05"
or
var search = "05",
rgx = new RegExp("^"+search),
leave_start = "05/12/2016";
var test = leave_start.match(rgx);
console.log(test); // ["05", index: 0, input: "05/06/2016"]
console.log(test[0]); // "05"
Another alternative is to use the aggregation framework and take advantage of the $substr operator to extract the first 2 characters of a field and then the $match operator will filter documents based on the new substring field above:
db.collection.aggregate([
{
"$project": {
"leaves_start": 1,
"monthSubstring": { "$substr": : [ "$leaves_start", 0, 2 ] }
}
},
{ "$match": { "monthSubstring": "05" } }
])
I want to use regex to get an arrayLike object from the params giving to url
e.g.
http://mysite/myPeople?one=jose&two=emily&three=john
basically what it does is this
function interpretUrl(url){
var reg = /\?([^&?=]*)=([^&?=]*)/g; //what am i doing wrong?
//some logic here
reg.exec(url)
return {
param: [
one: 'jose',
two: 'emily',
three: 'john'
],
root:
}
}
You can use this to get all the parameters from query string:
var re = /([^?&]+)=([^&]*)/g,
matches = {},
input = "http://mysite/myPeople?one=jose&two=emily&three=john";
while (match = re.exec(input.substr(input.indexOf('?')+1))) matches[match[1]] = match[2];
console.log(matches);
//=> {one: "jose", two: "emily", three: "john"}