Regular expression for field and value - regex

I've text as:
field = a value = 1345 field = b value = 3453
I need regular expression so that I can extract data as:
field value
a 1345
b 3453
What can be the perfect regex for it?

The expression should be:
field\s=\s([^\s]+)\svalue\s=\s([^\s]+)
Regex demo.
There are two capturing groups: one for field and one for value. Also note that I have specified any character other than space as a value for field or value - you could make it stricter.

Try this:
field\s*=\s*([^\s]+)\s*value\s*=\s*([^\s]+)
demo
it fill up more than one spaces case like field = a value = 1345 field = b value = 3453

Related

How to get same search result if i filter a value with hyphen or without hyphen or with white spaces in queryset field?

I need to get the same result when i search a word with or without a hyphen as well as white space in django queryset field.What should i do to satisfy this.
Eg. Queryset.objects.filter(type__icontains="T shirt").all()
The value i stored is "T-Shirts",
so how can i get this result from filtering
Can you explain your use case a little more?
You can try doing the following -
Store the value of type in a variable. I am assuming you are fetching the value of type from the request.
value = request.data.get("type")
value = value.replace(" ", "-")
queryset = Queryset.objects.filter(type=value).all()

How to extract a keyword from a string in Regex

For example: I want to extract the variable id from this string: validateId(id, name) && id=="123". However, I do not want to extract the word id from other places, such as in this case in validateId. Therefore, the result in this case would be: validateId(id, name) && id=="123"
I have tried matching on the opening brackets in validateId( however I am not managing to extract exactly what I need.
Note: id is just an example, I want to be able to accept any form of keywords. However, I can then change the regex accordingly to the keyword that I want.
I'm assuming that the keywords that you want to extract are always found in the same pattern. In this example I've added another.
test_str = '''validateId(id, name) && id=="123"
ParameterValue(Test, name) && Test=="123"'''
Match the pattern of the provided test string. This will find ìd and test.
reg_keyword = '(?<=\()(\w+)|(?<=&&\s)(\w+)'
re.findall returns a tuple in this case, where we are removing empty values and returning a list.
keyword = [k[0] for k in re.findall(reg_keyword, test_str) if len(k[0])>1]
Output
['id', 'Test']

How does the group in regular expression work when there is only one group?

I did the following:
a = re.compile("(ab*?)")
b = a.search("abbbbbbb")
The following gave me the answer 'a':
b.group(0)
Surprisingly, this also gave me the answer 'a':
b.group(1)
I get a tuple containing only ('a',) when I do this:
b.groups()
If there is only one group, why did it give repeated values for indices 0 and 1? Shouldn't it have raised an IndexError at 1?
help(b.group)
Help on built-in function group:
group(...) method of _sre.SRE_Match instance
group([group1, ...]) -> str or tuple.
Return subgroup(s) of the match by indices or names.
For 0 returns the entire match.
Regular expressions start numbering the capture groups at 1. Trying to access group 0 will give you the entire match (all groups), but since the expression has only one group the outputs are the same.
Example:
>>> regex = re.compile("(first) (second)")
>>> results = regex.search("first second")
>>> results.group(1)
'first'
>>> results.group(2)
'second'
>>> results.group(0)
'first second'

Regex for field and variable

I'm trying to match fields in a Java class using regex. So far it's been working great overall, but with just one issue. Once I match a field, I then have a second regex for matching the name of the field. I use (\w+)\s*(?:\s*=\s*[^;,]+)? to match the name of fields, but if the value of the field is wrapped in brackets with spaces separating values for that field, it starts matching field values as field names. For example, the below matches all the names called value#, however, once it gets to value5, the 2 is matched because of the space inside the brackets separating the field's value. I need a way to not match spaces while inside brackets, if possible, while still accomplishing what is currently matched with values 1-4. Even a solution that can match value 5 properly but will mess up with 6 and 7 would be a welcomed improvement.
Sample code:
value1;
value2 = 3;
value3 = 4, value4 = 4;
value5 = {
1, 2
};
value6 = {
1, 2
}, value7 = {
1, 2
};
Try this:
([a-zA-Z_]\w*)(?:\s*=\s*(?:\{[^\}]+\}|[\{;,]+))?
This expression also takes advantage of the fact that field names cannot begin with a digit.

can any one tell me regular expression for this? UPDATED

I tried many syntax in vistal studio, and in this site, but nothing helped.
The expression would be _ct_(anyDigitHere)_
like
adkasjflasdjfsdf asdfkl sjfdsf _ct150_ asdfasd // so it would match this _ct150
any thing here doens't matter Random stuff..afd a&r9qwr89 ((
_ct415487_ anything here doesn't matter // this will match _ct415487_
basically any _ctAndAnyNumberHere_ (underscore at start and end)
A couple I tried ^*(_ct)(:z)(+_)+*$, ^*(_ct[0-9]+_)+*$. But none helps!
EDIT
Thanks for the reply(ies). That did work, but what I now the problem is replace those matched elements with a hidden field value.. say..
if the value in hidden field is of 1 digit, (any value from 0-9), I have to take last digit from that matched expression and replace it with the value in hidden field.
if the value in hidden field is of 2 digit, (any value from 0-99), I have to take last two digits from that matched expression and replace it with the value in hidden field.
so, basically..
if the value in hidden field is of n digit, I have to take last n digits from that matched expression and replace it with the value in hidden field.
How do I do that?
I don't know what language of visual studio you're talking about, but this should work:
_ct\d+_
or this:
_ct([0-9]+)_
EDIT:
Regex rg = new Regex("_ct([0-9]+)_");
string text = "any thing here doens't matter Random stuff..afd a&r9qwr89 ((_ct415487_ anything here doesn't matter";
var match = rg.Match(text).Groups[1].Value;
int sizeHiddenField = HiddenField1.Value.Length;
var newString = text.Replace(match, match.Substring(0, match.Length - sizeHiddenField) + HiddenField1.Value);
/_ct\d*_/
This is the regular expression syntax for your given problem. Try this