检查字符串中的值的函数

时间:2018-07-30 10:54:22

标签: regex string substring

我正在使用一个尝试检查用户目录中用户位置的应用程序。

我的字符串类似于:

CN=Annette Luis Morgant,OU=Users,OU=CH,OU=Local,DC=domain,DC=application,DC=com

"OU"

我正在尝试在javascript中过滤字符串,以便仅输出 second "NA"的值。 因此,第一种情况为"CH",第二种情况为public class SplitUser { public static void main(String[] args) { String MyStringContent = "CN=John Mayor,OU=Users,OU=NA,OU=Local,DC=domain,DC=application,DC=com"; String[] arrSplit = MyStringContent.split(","); for (int i=0; i < arrSplit.length; i++) { System.out.println(arrSplit[i]); } //System.out.println(arrSplit[2]); String p = arrSplit[2].substring(3, arrSplit[2].length()); System.out.println(p); }}

试图使用子字符串和修剪或类似的东西,但是我感到困惑! 你能帮助我吗? 谢谢!!!

编辑----- 这就是我想要做的:

struct CellData {
    var isOpened = Bool()
    var title = String()
    var sectionData = [String]()
}

class TableViewController: UITableViewController {
    var tableViewData = [CellData]()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem

        tableViewData = [CellData(isOpened: false, title: "title1", sectionData: ["cell1","cell2","cell3"]),CellData(isOpened: false, title: "title2", sectionData: ["cell1","cell2","cell3"]),CellData(isOpened: false, title: "title3", sectionData: ["cell1","cell2","cell3"])]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return tableViewData.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if tableViewData[section].isOpened {
            return tableViewData[section].sectionData.count + 1
        }
        return 1
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
                return UITableViewCell()
            }
            cell.textLabel?.text = tableViewData[indexPath.section].title
            return cell
        } else {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
                return UITableViewCell()
            }
            cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row - 1]
            return cell
        }
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            if tableViewData[indexPath.section].isOpened {
                tableViewData[indexPath.section].isOpened = false
            }else {
                tableViewData[indexPath.section].isOpened = true
            }
            let sections = IndexSet(integer: indexPath.section)
            tableView.reloadSections(sections, with: .none)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

(?:,|^)OU=[^,]+(?=.*?,OU=([^,]+)).*

即使在“ OU”中插入了其他一些FOO = BAR值,它也将起作用

解释:

(?:,|^)  # start by begin of line or ','
OU=   
[^,]+    #Anything but a ',' 1 or more times
         #We have found 1 OU, let's find the next 
(?= # Lookahead expression
    .*? # Anything (ungreedy)
    ,OU=
    ([^,]+)
)
.* # Anything, just to match the whole line 
   # and avoid multiple matches for the same line (g flag)

演示here