I have a line like this
SNMPv2-SMI::enterprises.6889.2.69.5.1.58.0 = IpAddress: 10.169.130.48 SNMPv2-SMI::enterprises.6889.2.69.5.1.52.0 = STRING: "999"
and I want select the ip address (10.169.130.48) and the string output (999) and trim everything else and I am using this code.
/.*\s=\sIpAddress:\s(\d+\.\d+\.\d+\.\d+)\sSNMPv2-SMI.*\s=\sSTRING:\s\"(\d+)\"/
but I only get 10.169.130.48 not the string out put. my question is I can't use two () to select what I want? what is the other option?
Your regex is correct, you are just missing the result. Check this out for a solution!
Your regex is fine. The only thing is, in Perl you don't need to escape the double quotes characters. Since the forward slash notation is not interpreted as a string.
You can access the second capture through $2
Following regex works in Javascript:
var s = 'SNMPv2-SMI::enterprises.6889.2.69.5.1.58.0 = IpAddress: 10.169.130.48 SNMPv2-SMI::enterprises.6889.2.69.5.1.52.0 = STRING: "999"'
var m = s.match(/\bIpAddress:\s*([^\s]+).*?\bSTRING:\s*([^\s]+)/);
// m[1] = 10.169.130.48
// m[2] = "999"
Related
I am trying to get string using RegEx; here is the string:
window.runParams = {};
window.runParams = {blablabla};
How to get the second string {blablabla}? I am using REGEX:
(?<=window.runParams = ").*(?=;)
But that gets the first string {}.
If you want to get string with braces eg: {blablabla}
window.runParams = ({\w+})
If you want to get only the string inside braces eg: blablabla
window.runParams = {(\w+)}
Value of group 1 is your result
The following pattern captures only curly brackets with word character content:
(?<=window.runParams = ){\w+}(?=;)
and will only capture:
{blablabla}
when run against the text:
window.runParams = {};
window.runParams = {blablabla};
See results here:
https://regex101.com/r/mTwA64/1
try modifying your regex so it only accepts matches with non-empty curly brackets \{.+\} such as
(?<=window\.runParams = )(\{.+\})(?=;)
...there's probably ways to simplify the regex further, depending on you problem...my guess is you don't need the lookahead/lookbehind, e.g. in the example given \{.+\} will do just fine (returns {blablabla}) ....but it really depends on the format and content of your file...also remember braces, dots etc have a special meaning in regexes so you probably would want to escape them
I have a pattern such as word-\nword, i.e. words are hyphenated and separated by new line character.
I would like the output as word-word. I get word-\nword with the below code.
text_string = "word-\nword"
result=re.findall("[A-Za-z]+-\n[A-Za-z]+", text_string)
print(result)
I tried this, but did not work, I get no result.
text_string = "word-\nword"
result=re.findall("[A-Za-z]+-(?=\n)[A-Za-z]+", text_string)
print(result)
How can I achieve this.
Thank You !
Edit:
Would it be efficient to do a replace and run a simple regex
text_string = "aaa bbb ccc-\nddd eee fff"
replaced_text = text_string.replace('-\n', '-')
result = re.findall("\w+-\w+",replaced_text)
print(result)
or use the method suggested by CertainPerformance
text_string = "word-\nword"
result=re.sub("(?i)(\w+)-\n(\w+)", r'\1-\2', text_string)
print(result)
You should use re.sub instead of re.findall:
result = re.sub(r"(?<=-)\n+", "", test_str)
This matches any new lines after a - and replaces it with empty string.
Demo
You can alternatively use
(?<=-)\n(?=\w)
which matches new lines only if there is a - before it and it is followed by word characters.
If the string is composed of just that, then a pure regex solution is to use re.sub, capture the first word and the second word in a group, then echo those two groups back (without the dash and newline):
result=re.sub("(?i)([a-z]+)-\n([a-z]+)", r'\1\2', text_string)
Otherwise, if there is other stuff in the string, iterate over each match and join the groups:
text_string = "wordone-\nwordtwo wordthree-\nwordfour"
result=re.findall("(?i)([a-z]+)-\n([a-z]+)", text_string)
for match in result:
print(''.join(match))
You can simply replace any occurrences of '-\n' with '-' instead:
result = text_string.replace('-\n', '-')
Kapil Arora <kapil.arora#abc.in>
How to find the name before angular bracket
This is the RegEx I used ([^<]+). but it is not finding first String
I would just add a start of input anchor ^ to the head of your expression, plus a look ahead for a space so you get just the name (no trailing space):
^[^<]+(?= )
No need for brackets; group 0 is the whole match, which is what you want.
See live demo.
Since you haven't specified any language. I would be solving in JavaScript.
Lets assume an email id as "kapil.sharma123#gmail.com".
Thus the program would be something like this:
var email = "kapil.sharma123#gmail.com";
var regex = /(^[A-Za-z]+\.+[A-Za-z]+)/;
var res = email.match(regex)[1];
res = res.split(".").join(" ");
Here I am matching the regex with the email id string and then extracting from the first index.
Finally I am spliting on "." and joining with a blankspace.
Note : It also works for simple email ids like "kapil.sharma#gmail.com"
You may try this:
const regex = /^\s*([^<]+)\s*</g;
const str = `Kapil Arora <kapil.arora#abc.in> bla bla bla <asadfasdf>`;
var match = regex.exec(str);
console.log(match[1].trim());
I want to extract a portion of a string, allowing for the dash character to appear randomly throughout. In my match, I want the dash character occurrences to be included.
Let's say I have a scenario like so:
haystack = "arandomse-que-nce"
needle = "sequence"
and I want to come out on the other end with a string like se-que-nce this this case, what would the regex pattern look like?
I would split the string and then join by -*; for example, in JavaScript:
var needle = "sequence"
var regex = new RegExp(needle.split('').join('-*'))
var result = "arandomse-que-nce".match(regex) // ["se-que-nce"]
var result2 = "a-bad-sequ_ence".match(regex) // null
You could also use a regex to insert -* between each character:
var regex = new RegExp(needle.replace(/(?!$|^)/g, '-*'))
Both the split/join method and the replace method return 's-*e-*q-*u-*e-*n-*c-*e' for the regex.
If you have characters like * in your string, that have meanings in regular expressions, you may want to escape them, like so:
var regex = new RegExp(needle.replace(/(?!$|^)/g, '-*')
.replace(/([-\\^$*+?.()|[\]{}])/g, '\\$1'))
Then, if needle was 1+1, for example, it would give you 1-*\+-*1 for the regex.
s-*e-*q-*u-*e-*n-*c-*e-*
The assumes that multiple hyphens in a row are okay.
EDIT: Doorknob's split/join solution is good, but be aware that it only works for character that aren't special characters (*, +, etc.)
I don't know what the specifications are, but if there are special characters, make sure to escape them:
new RegExp(needle.split('').map(function(c) { return '\\' + c; }).join('-*'))
You could try to use:
s-?e-?q-?u-?e-?n-?c-?e
I'd like to capture the value of the Initial Catalog in this string:
"blah blah Initial Catalog = MyCat'"
I'd like the result to be: MyCat
There could or could not be spaces before and after the equal sign and there could or could not be spaces before the single quote.
Tried this and various others but no go:
/Initial Catalog\s?=\s?.*\s?\'/
Using .Net.
You need to put parentheses around the part of the string that you would like to match:
/Initial Catalog\s*=\s*(.*?)\s*'/
Also you would like to exclude as many spaces as possible before the ', so you need \s* rather than \s?. The .*? means that the extracted part of the string doesn't take those spaces, since it is now lazy.
This is a nice regex
= *(.*?) *'
Use the idea and add \s and more literal text as needed.
In C# group 1 will contain the match
string resultString = null;
try {
Regex regexObj = new Regex("= *(.*?) *'");
resultString = regexObj.Match(subjectString).Groups[1].Value;
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
Regex rgx = new Regex(#"=\s*([A-z]+)\s*'");
String result = rgx.Match(text).Groups[1].Value;