리눅스맨

c# 숫자만 입력

c# 숫자만 입력 textbox string 문자열 정규표현식 영어 숫자만 가져오기

※ 위 배너를 클릭하면 1달러에 (900원~1800원) 살 수 있는 가성비 레어템 많습니다.

C# 숫자만 입력 하기 위해서 정규표현식을 사용하여 알고리즘을 만들었습니다.

제가 필요한 부분은 이렇게 많은 영어와 숫자중에서 grp-a001-01-000000xxxxxxx 로 시작하는 항목만 필요하였으며 이를 정규표현식으로 아래처럼 만들었습니다.

c# 숫자만 입력

그냥 드래그로 쭈욱~~ 복사하고 이것을 프로그램에 입력하면 알아서 grp-a001-01-000000xxxxxxx로 되어진 항목만 빼오는것이죠.

아래처럼 왼쪽 textBox 항목에 위의 클립아트 text를 붙여넣으면 알아서 원하는 패턴 항목만 가져옵니다.

c# 숫자만 입력

C# 소스파일 다운받기

grp-a001-01-0000000000000.zip

Regex.Matches(“검색할문자열”, pattern)

패턴에 아래 [ 괄호 안 ] 내용을 입력하여 사용하면됩니다.

위의 소스코드 다운받아서 직접 해보시면 어떻게 동작하는지 알 수 있습니다.

c# 숫자만 입력 C#숫자만 가져오기

[0-9]

C#영어만 가져오기 (대소문자 모두 가져옵니다)

[a-zA-Z]

C#숫자 영어 문자만 가져오기

[a-zA-Z0-9]

간단하게 린크드리스트(LinkedList) 와 정규표현식(RegularExpressions )을 사용하였습니다.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace grp_a001_01_0000000000000
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
//grp-a001-01-000000023156551

LinkedList<string> linkedList_groupStringAll = new LinkedList<string>();
LinkedList<string> linkedList_groupStringUnique = new LinkedList<string>();

string pattern = "(grp-)(?<groupString>[a-zA-Z0-9]{4}-[0-9]{2}-[0-9]{15}?)";
MatchCollection matches = Regex.Matches(textBox1.Text, pattern);

textBox2.Text = string.Empty;
int matchesIndex = 0;
foreach (Match match in matches)
{

if (match.Groups["groupString"].Length > 0)
{
linkedList_groupStringAll.AddLast(match.Groups["groupString"].Value); 
}
}

foreach (string groupListEachItem in linkedList_groupStringAll)
{
int compareIndex = 0;
foreach (string groupListEachItem_compare in linkedList_groupStringAll)
{
if(groupListEachItem.Equals(groupListEachItem_compare))
{
if(compareIndex++ > 2)
{
break;
}
}
}

if(compareIndex == 1)
{
linkedList_groupStringUnique.AddLast(groupListEachItem);
}
}


matchesIndex = 0;
foreach (string groupListEachItem in linkedList_groupStringUnique)
{
if (++matchesIndex == linkedList_groupStringUnique.Count)
{
textBox2.AppendText(string.Format("{0}", groupListEachItem));
}
else
{
textBox2.AppendText(string.Format("{0}\r\n", groupListEachItem));
}
}

textBox2.AppendText(string.Format("\r\n총:{0,0}개", linkedList_groupStringUnique.Count));


}
}
}

전체 소스파일은 제가 올려놓았으니 필요하신분들은 다운받아서 사용해보시기 바랍니다.


게시됨

카테고리

작성자

태그: