Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Pulling some trading data and having issues using regex to separate tickers and percentage of holding
Inputs
"94324.13%"
"007007.13%"
"0354202.91%"
Desired Output
"9432|4.13%" (ticker is 4 numbers)
"00700|7.13%" (ticker is 5 numbers)
"035420|2.91%" (ticker is 6 numbers)
Main issue is that the number of digits the ticker is may vary anywhere from 4-6 digits.
With the given information it is not possible to have a 100% accurate split of the two parts. For instance:
123410.05%
... could split in either of the following two:
1234|10.05%
12341|0.05%
And if percentages might not have a zero before the decimal point, then this would also be a possible split:
123410|.05%
The following regex replace will assume the percentage has one digit before the decimal point, and possibly a minus sign:
Find:
/^(\d{4,6})(\-?\d.*)$/gm
Replace:
\1|\2
See it on regex101.com.
I'd like to try this regex
(\d{4,6})(\d+\.\d{1,2}%)
Here is full demo:
Python:
data = "007007.13%"
rx = re.compile(r"(\d{4,6})(\d+\.\d{1,2}%)")
formated_text = rx.sub(r'\1|\2', data)
print formated_text
#it will print
00700|7.13%
You can look demo in python here
Javascript:
var re = /(\d{4,6})(\d+\.\d{1,2}%)/g;
var str = '"007007.13%"';
var subst = '$1|$2';
var result = str.replace(re, subs);
Demo in Javascript
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to validate a 5 digit number.
Max of 5 digits
First 2 digits are within various ranges like (00-16) or (20-25) or
(32-39)
00789 - valid
11569 - valid
22698 - valid
32567 - valid
17895 - not valid
41578 - not valid
Is there a Regex Guru that can help with a regex expression that would work for this scenario?
I don't know anything about regular expressions. This a small part of a bigger solution that has legacy code using regular expression strings as data validation. A number comes in as a parameter. A lookup is done to get a regex validation string. The number and the regex string is passed to a validator where a regex.IsMatch is performed.
My question is can the above validation senario be written in a regex expression, if so what would that look like? I could then add the expression to the existing library of regex expressions in my app.
Why regex? First you need a collection to store your ranges, for example:
Dim ranges = New List(Of Tuple(Of Int32, Int32))
ranges.Add(Tuple.Create(0, 16))
ranges.Add(Tuple.Create(20, 25))
ranges.Add(Tuple.Create(32, 39))
The check itself is pretty easy:
Dim firstTwo = text.TrimStart("0"c).Substring(0, 2)
Dim number As Int32
Dim isValid = Int32.TryParse(firstTwo, number) AndAlso
ranges.Any(Function(t) number >= t.Item1 AndAlso number <= t.Item2)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have the following sentence:
b.g The big bag of bits was bugged.
How can I exclude the b.g from it by using a regular expression?
I am sure I need a negative lookahead but I cannot get it right yet.
Something like
^(?!b\.g)
I would do it this way:
[^\S].*
What [^\S] does is basically skip any character until it reaches the first space. then start capturing. No need in this case for negative or positing Lookbehind.
Demo: regex101
If you prefer to do it with positive Lookbehind, you can do it this way
(?<=b\.g).*
Demo: regex101
sed 's/^...//' strips the first 3 characters, "b.g", but I doubt that's what you're really asking. Your ^ anchor appears to be a red herring.
You already have correct escaping for . period, just stick with that:
sed 's/b\.g//'
Python's positive lookbehind ?<= may be what you are trying to find words to express:
>>> m = re.search(r'(?<=b\.g)(.*)', 'b.g The big bag of bits was bugged.')
>>> print(m.group(1))
The big bag of bits was bugged.
In python you could do something like this:
import re
w = 'b.g The big bag of bits was bugged.'
print w
d = re.compile(r'^b.g\s')
a = re.sub(d, '', w)
print a
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need a regex to extract the number 330 on ...163&angleKreludor=330&viewID=.... This number vary from day to day...it could be an Integer with up to 3 digits, but it can be a double number like 127.57 with 2 decimal places ... so I would need to capture anything between angleKreludor= and &viewID.... Here is the complete HTML:
var swf = new SWFObject('http://images.lulaser.com/shenkuu/lunar/shenkuu_calendar_v1.swf?angleNeopia=163&angleKreludor=330&viewID=2&lang=pt', 'flash_36175654223', '550', '500', '6', '#FFFFFF');
swf.addParam('quality', 'high');
swf.addParam('scale', 'exactfit');
swf.addParam('menu', 'false');
swf.addParam('allowScriptAccess', 'always');
swf.addParam('swLiveConnect', 'true');
swf.addParam('bgcolor', 'white');
swf.write();
P.S: This is needed to use in Javascript code in Selenium IDE ... I tried in the past and Selenium IDE does not accept look-aheads nor look-behinds
You can search for digits with positive look-behind for the angleKreludor= key
(?<=angleKreludor=)(\d+)
DEMO
For JavaScript use non-captuinrg group
(?:angleKreludor=)(\d+)
var s = 'http://images.lulaser.com/shenkuu/lunar/shenkuu_calendar_v1.swf?angleNeopia=163&angleKreludor=330&viewID=2&lang=pt';
var nr = s.match(/(?:angleKreludor=)(\d+)/);
console.log(nr[1]);
DEMO
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I don't really know how to use regex, and I have a task to get bulk image downloader to find a set amount of pages for example pages 1-20 to link crawl.
This is the URL:
/index.php?app=core&module=search&do=viewNewContent&period=month&userMode=&search_app=forums&sid=ceb2a9ba4039e4a06d3a6775aa735f2d&search_app_filters[forums][searchInKey]=&st=400
Its page (the st param) is incremented in +25 so the following page would be:
/index.php?app=core&module=search&do=viewNewContent&period=month&userMode=&search_app=forums&sid=ceb2a9ba4039e4a06d3a6775aa735f2d&search_app_filters[forums][searchInKey]=&st=425
How can I match and replace the page number with the next consecutive page number?
You can just capture the last digits and use whatever language you're writing in to increment that by 25:
/(\/index\.php.+?)(\d+)$/
This will give you the URL in $1 and the page number in $2 or matches[2] (however your language of choice represents the first "capture"). With that, you can increment it.
This Ruby example will do that:
matches = url.match(/(\/index\.php.+?)(\d+)$/)
page = matches[2].to_i # Convert the page number to integer
page = page + 25 # Calculate the new page number
new_url = matches[1] + (page).to_s # Merge in the new page number
That should do it for this format of URL.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to use regex so as to obtain specific information from the text and I give an example with a semi-pseudocode ~ you can also reply me with semi-pseudocode:
list=["orange","green","grey"]
text= "The Orange is orange"
for word in list:
if word == re.compile(r'word, text):
capture Orange in order to have the noun
Beware! My question focuses whether there is a possibility to use variables (as word up above) so as to make a loop and see if there are equal words in an text based on a list.
Do not focus on how to capture the Orange.
I think Biffen has the right idea, you're in a world of pain if you're using this for POS tagging. Anyway, this allows you to match words in your text variable
for word in list:
if word in text:
# Do what you want with word
If you wanted to use regex then you can build patterns from strings, use parentheses to capture. Then use group() to access captured patterns
for word in list:
pattern = re.compile(".*(" + word + ").*")
m = re.match(pattern, text)
if m:
print(m.group(1))