admin管理员组文章数量:1401947
Using Google Sheets: I'm trying to extract the price e.g. 250, 990 and 1760, the currency e.g. CZK, and problematically the text /p, /2p or /3p from some text field. Some example lines are shown below.
250CZK /p /night
Extract in three cells 250, CZK, and /p
990CZK /night /2p room (/3p)
Extract in three cells 990, CZK, and /2p
1760CHF /3p, 1375 /2p
Extract in three cells 1760, CHF, and /3p
In the third example the 1375 /2p can be ignored. I only need the first instance. The sequence order of the entries can be random, e.g. I'd like the formula to be able to handle strings like:
/3p /night USD 550
resulting in
Extract in three cells 550, USD, and /3p
Using Google Sheets: I'm trying to extract the price e.g. 250, 990 and 1760, the currency e.g. CZK, and problematically the text /p, /2p or /3p from some text field. Some example lines are shown below.
250CZK /p /night
Extract in three cells 250, CZK, and /p
990CZK /night /2p room (/3p)
Extract in three cells 990, CZK, and /2p
1760CHF /3p, 1375 /2p
Extract in three cells 1760, CHF, and /3p
In the third example the 1375 /2p can be ignored. I only need the first instance. The sequence order of the entries can be random, e.g. I'd like the formula to be able to handle strings like:
/3p /night USD 550
resulting in
Extract in three cells 550, USD, and /3p
1 Answer
Reset to default 0You want to analyse a number of strings that include a currency abbreviation, currency Value and per person value. Where there are more than one of any of these in any string, you want to return the first matching value.
Consider this script:
Logic
- the strings are taken from the spreadsheet and a loop is created to process each string
- An array,
currencies
, contains all the relevant currency codes. - Each string value is analysed for:
- Currency Code: a loop is created to process all currnecies; and
indexOf
is used to identify a match. - Currency value: The Javascript method String.prototype.match is used with regex value =
\d{3,4}
to find a number consisting of 3 or 4 digits. - Per person value:
String.prototype.match
is used again with regex =/\d?p/
- the regex searches for the first instance of "/" and "p" and any digits between the two characters.
- Currency Code: a loop is created to process all currnecies; and
- Results are pushed onto a temporary array
- The temporary array is used to
setValues
to the spreadsheet.
function analyseString() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName("Sheet3")
var range = sheet.getRange("A3:A6")
var stringValues = range.getValues()
// get currency
var currencies = ["CZK", "CHF", "USD", "EUR", "GBP"]
// create a temportary array to hold results
var results = []
// loop through the stringValues
for (var v=0;v<stringValues.length;v++){
var string = stringValues[v][0]
// loop through currencies and get a match
for (var i=0;i<currencies[0].length;i++){
var currencyCode = currencies[i]
var searchIdx = string.indexOf(currencyCode)
if (searchIdx !=-1){
break
}
}
// get the Currency value
var unitsValue = string.match(/\d{3,4}/);
// get the perPerson value
const persons = string.match(/\/\d?p/);
Logger.log("v = "+v+", string value = "+string+", Unit value = "+unitValue+", Currency Code= "+currencyCode+", per persons = "+persons)
// push results to a temporary array
results.push([unitsValue,currencyCode,perPerson])
}
sheet.getRange(3,3,results.length,3).setValues(results)
}
SAMPLE - BEFORE
SAMPLE - console log
v = 0,
string value = 250CZK /p /night,
Unit value = 250,
Currency Code= CZK,
per persons = /pv = 1,
string value = 990CZK /night /2p room (/3p),
Unit value = 990,
Currency Code= CZK,
per persons = /2pv = 2,
string value = 1760CHF /3p, 1375 /2p,
Unit value = 1760,
Currency Code= CHF,
per persons = /3pv = 3,
string value = /3p /night USD 550,
Unit value = 550,
Currency Code= USD,
per persons = /3p
SAMPLE - AFTER
本文标签:
版权声明:本文标题:regex - Seeking to extract certain including ""n"p" where n is a digit or may not exist - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744328298a2600834.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
CZK
,CHF
andUSD
appear to be currency abbreviations. is this correct? Is there a finite list of currencies that is used? – Tedinoz Commented Mar 23 at 6:52